Javascript – 在数组的一个属性上按字母顺序排列对象
假设你有一个这样的JavaScript类
var DepartmentFactory = function(data) { this.id = data.Id; this.name = data.DepartmentName; this.active = data.Active; }
比方说,然后创build该类的一些实例,并将它们存储在一个数组中
var objArray = []; objArray.push(DepartmentFactory({Id: 1, DepartmentName: 'Marketing', Active: true})); objArray.push(DepartmentFactory({Id: 2, DepartmentName: 'Sales', Active: true})); objArray.push(DepartmentFactory({Id: 3, DepartmentName: 'Development', Active: true})); objArray.push(DepartmentFactory({Id: 4, DepartmentName: 'Accounting', Active: true}));
所以我现在将有一个由DepartmentFactory
创build的对象的数组。 我将如何去使用array.sort()
方法按每个对象的DepartmentName
属性对这个对象的数组进行sorting?
在sortingstring数组时, array.sort()
方法工作得很好
var myarray=["Bob", "Bully", "Amy"]; myarray.sort(); //Array now becomes ["Amy", "Bob", "Bully"]
但是,如何使它与对象列表一起工作呢?
你将不得不做这样的事情:
objArray.sort(function(a, b) { var textA = a.DepartmentName.toUpperCase(); var textB = b.DepartmentName.toUpperCase(); return (textA < textB) ? -1 : (textA > textB) ? 1 : 0; });
注意:更改大小写(大写或小写)确保不区分大小写。
为了支持unicode:
objArray.sort(function(a, b) { return a.DepartmentName.localeCompare(b.DepartmentName); });
var DepartmentFactory = function(data) { this.id = data.Id; this.name = data.DepartmentName; this.active = data.Active; } // use `new DepartmentFactory` as given below. `new` is imporatant var objArray = []; objArray.push(new DepartmentFactory({Id: 1, DepartmentName: 'Marketing', Active: true})); objArray.push(new DepartmentFactory({Id: 2, DepartmentName: 'Sales', Active: true})); objArray.push(new DepartmentFactory({Id: 3, DepartmentName: 'Development', Active: true})); objArray.push(new DepartmentFactory({Id: 4, DepartmentName: 'Accounting', Active: true})); function sortOn(property){ return function(a, b){ if(a[property] < b[property]){ return -1; }else if(a[property] > b[property]){ return 1; }else{ return 0; } } } //objArray.sort(sortOn("id")); // because `this.id = data.Id;` objArray.sort(sortOn("name")); // because `this.name = data.DepartmentName;` console.log(objArray);
演示: http : //jsfiddle.net/diode/hdgeH/
// Sorts an array of objects "in place". (Meaning that the original array will be modified and nothing gets returned.) function sortOn (arr, prop) { arr.sort ( function (a, b) { if (a[prop] < b[prop]){ return -1; } else if (a[prop] > b[prop]){ return 1; } else { return 0; } } ); } //Usage example: var cars = [ {make:"AMC", model:"Pacer", year:1978}, {make:"Koenigsegg", model:"CCGT", year:2011}, {make:"Pagani", model:"Zonda", year:2006}, ]; // ------- make ------- sortOn(cars, "make"); console.log(cars); /* OUTPUT: AMC : Pacer : 1978 Koenigsegg : CCGT : 2011 Pagani : Zonda : 2006 */ // ------- model ------- sortOn(cars, "model"); console.log(cars); /* OUTPUT: Koenigsegg : CCGT : 2011 AMC : Pacer : 1978 Pagani : Zonda : 2006 */ // ------- year ------- sortOn(cars, "year"); console.log(cars); /* OUTPUT: AMC : Pacer : 1978 Pagani : Zonda : 2006 Koenigsegg : CCGT : 2011 */
DEMO
var DepartmentFactory = function(data) { this.id = data.Id; this.name = data.DepartmentName; this.active = data.Active; } var objArray = []; objArray.push(new DepartmentFactory({Id: 1, DepartmentName: 'Marketing', Active: true})); objArray.push(new DepartmentFactory({Id: 2, DepartmentName: 'Sales', Active: true})); objArray.push(new DepartmentFactory({Id: 3, DepartmentName: 'Development', Active: true})); objArray.push(new DepartmentFactory({Id: 4, DepartmentName: 'Accounting', Active: true})); console.log(objArray.sort(function(a, b) { return a.name > b.name}));
你必须传递一个接受两个参数的函数,比较它们,然后返回一个数字,所以假设你想用ID对它们进行sorting,你会写…
objArray.sort(function(a,b) { return a.id-b.id; }); // objArray is now sorted by Id
像这样做
objArrayy.sort(function(a, b){ var nameA=a.name.toLowerCase(), nameB=b.name.toLowerCase() if (nameA < nameB) //sort string ascending return -1 if (nameA > nameB) return 1 return 0 //default return value (no sorting) }); console.log(objArray)
一个简单的答案:
objArray.sort(function(obj1, obj2) { return obj1.DepartmentName > obj2.DepartmentName; });
ES6方式:
objArray.sort((obj1, obj2) => {return obj1.DepartmentName > obj2.DepartmentName};
如果你需要使它成为小写/大写等,只要做到这一点,并将结果存储在一个variables比比较该variables。 例:
objArray.sort((obj1, obj2) => { var firstObj = obj1.toLowerCase(); var secondObj = obj2.toLowerCase(); return firstObj.DepartmentName > secondObj.DepartmentName; });