1. 程式人生 > >js 根據陣列中物件的某一個屬性值進行排序

js 根據陣列中物件的某一個屬性值進行排序

 var arr = [
    {name:'zopp',age:0},
    {name:'gpp',age:18},
    {name:'yjj',age:8}
];
 compare = (property) => {
    return function(a,b){
        var value1 = a[property];
        var value2 = b[property];
        return value1 - value2;
   	}
}
 arr.sort(this.compare('age'))