假设你有一个这样的JavaScript类
var DepartmentFactory = function(data) {
this.id = data.Id;
this.name = data.DepartmentName;
this.active = data.Active;
}
假设您随后创建了该类的许多实例,并将它们存储在一个数组中
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创建的对象数组。如何使用array.sort()方法按每个对象的DepartmentName属性对这个对象数组进行排序?
array.sort()方法在对字符串数组排序时工作得很好
var myarray=["Bob", "Bully", "Amy"];
myarray.sort(); //Array now becomes ["Amy", "Bob", "Bully"]
但是我如何让它与对象列表一起工作呢?
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
*/
在这个问题上做了一些尝试,并尽量减少循环之后,我最终得到了这个解决方案:
codeen上的演示
const items = [
{
name: 'One'
},
{
name: 'Maria is here'
},
{
name: 'Another'
},
{
name: 'Z with a z'
},
{
name: '1 number'
},
{
name: 'Two not a number'
},
{
name: 'Third'
},
{
name: 'Giant'
}
];
const sorted = items.sort((a, b) => {
return a[name] > b[name];
});
let sortedAlphabetically = {};
for(var item in sorted) {
const firstLetter = sorted[item].name[0];
if(sortedAlphabetically[firstLetter]) {
sortedAlphabetically[firstLetter].push(sorted[item]);
} else {
sortedAlphabetically[firstLetter] = [sorted[item]];
}
}
console.log('sorted', sortedAlphabetically);
这是一个简单的函数,你可以用它来排序数组对象的属性;不管属性是字符串类型还是整数类型,它都可以工作。
var cars = [
{make:"AMC", model:"Pacer", year:1978},
{make:"Koenigsegg", model:"CCGT", year:2011},
{make:"Pagani", model:"Zonda", year:2006},
];
function sortObjectsByProp(objectsArr, prop, ascending = true) {
let objectsHaveProp = objectsArr.every(object => object.hasOwnProperty(prop));
if(objectsHaveProp) {
let newObjectsArr = objectsArr.slice();
newObjectsArr.sort((a, b) => {
if(isNaN(Number(a[prop]))) {
let textA = a[prop].toUpperCase(),
textB = b[prop].toUpperCase();
if(ascending) {
return textA < textB ? -1 : textA > textB ? 1 : 0;
} else {
return textB < textA ? -1 : textB > textA ? 1 : 0;
}
} else {
return ascending ? a[prop] - b[prop] : b[prop] - a[prop];
}
});
return newObjectsArr;
}
return objectsArr;
}
let sortedByMake = sortObjectsByProp(cars, "make"); // returns ascending order by its make;
let sortedByYear = sortObjectsByProp(cars, "year", false); // returns descending order by its year,since we put false as a third argument;
console.log(sortedByMake);
console.log(sortedByYear);
因为这里给出的所有解决方案都没有null/undefined安全操作,所以我用这种方式处理(你可以根据自己的需要处理null):
ES5
objArray.sort(
function(a, b) {
var departmentNameA = a.DepartmentName ? a.DepartmentName : '';
var departmentNameB = b.DepartmentName ? b.DepartmentName : '';
departmentNameA.localeCompare(departmentNameB);
}
);
ES6+
objArray.sort(
(a: DepartmentFactory, b: DepartmentFactory): number => {
const departmentNameA = a.DepartmentName ? a.DepartmentName : '';
const departmentNameB = b.DepartmentName ? b.DepartmentName : '';
departmentNameA.localeCompare(departmentNameB);
}
);
我还删除了其他人使用的toLowerCase,因为localeCompare是不区分大小写的。另外,在使用Typescript或ES6+时,我更喜欢在参数上更明确一点,以便对未来的开发人员更明确。