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

时间:2023-03-09 22:00:29
使用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);

结果如下:使用sort对数组中的对象的某个属性进行排序

相关文章