对象按照某一属性排序
Contents
使对象按照某个顺序排序
当前属性值相同时按照另一个属性排序
let obj=[
{name:'zhangsan',age:20,score:89},
{name:'zhaoli',age:20,score:90},
{name:'lisi',age:21,score:86},
{name:'wangwu',age:23,score:95},
]
function rank(attr1,attr2) {
return function (a,b) {
if (a[attr1]==b[attr1]) return a[attr2]-b[attr2];
return a[attr1]-b[attr1];
}
}
obj.sort(rank('age'));
console.log(obj.sort(rank('age','score')));