根据数组中某一项进行排序
var arr = [
{name:'LL',age:5},
{name:'XX',age:24},
{name:'YY',age:16},
{name:'ZZ',age:10},
{name:'AA',age:26}
];
function compare(value){
return function(a,b){
var aaa = a[value];
var bbb = b[value];
return aaa - bbb;
}
}
console.log(arr.sort(compare('age')))
image.png
网友评论