sort方法接收一个函数作为参数,这里嵌套一层函数用来接收对象属性名,其他部分代码与正常使用sort方法相同.
var arr = [
{name:'aaa',age:0},
{name:'bbb',age:15},
{name:'ccc',age:6}
];
function compare(property){
return function(a,b){
var value1 = a[property];
var value2 = b[property];
return value1 - value2;
}
}
console.log(arr.sort(compare('age')))
反转数组 reverse()
console.log(arr.sort(compare('age')).reverse())