使用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);

结果如下:


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM