Cherish_Now

导航

使用sort对数组中的对象的某个属性进行排序

var data=[
{
    code: "10004",
    grade: "5.6",
    planQuantity: 220,
},
{
    code: "10024",
    grade: "5.6",
    planQuantity: 200,
},
{
    code: "10034",
    grade: "6.6",
    planQuantity: 210,
}
];
function orderSort(obj1,obj2){
    var a=obj1.planQuantity;
    var b=obj2.planQuantity;                                                    
    if(a>b){    
        return -1
    }else if(a<b){    
        return 1
    }else{
        return 0
    }              
};
data.sort(orderSort);

结果如下:

posted on 2019-04-19 11:38  Cherish_Now  阅读(1372)  评论(0编辑  收藏  举报