js 原生对象排序

时间:2021-02-20 08:27:48
//对象属性排序
function compare(propertyName) {
return function (object1, object2) {
var value1 = object1[propertyName];
var value2 = object2[propertyName];
if (value2 < value1) {
return -1;
}
else if (value2 > value1) {
return 1;
}
else {
return 0;
}
}
}
var data = obj[0]["day"];
data.sort(compare("time")).reverse();

var obj = [{
"name":"T",
"day":[
{
"time":"3",
"price":"30"
},
{
"time":"5",
"price":"50"
},
{
"time":"6",
"price":"60"
},
{
"time":"7",
"price":"70"
}
]
}
]

原声数据排序