js sort方法根据数组中对象的某一个属性值进行排序

转自 https://blog.csdn.net/qq_17335153/article/details/52574531

var arr = [
    {name:'zopp',age:0},
    {name:'gpp',age:18},
    {name:'yjj',age:8}
];

function compare(property){
    return function(a,b){
        var value1 = a[property];
        var value2 = b[property];
        return value1 - value2;
    }
}
console.log(arr.sort(compare('age')))

 

你可能感兴趣的:(js sort方法根据数组中对象的某一个属性值进行排序)