js 对数组对象进行排序

let listData=[{id:1,name:"测试1",presenttime:1557883600000},{id:2,name:"测试2",presenttime:1580751813000},{id:
3,presenttime:1561448381000}];

console.log("排序前:",listData);
//根据时间进行排序
listData = listData.sort(compare("presenttime"));
console.log("排序后:",listData);




//对数组进行排序
function compare(property) {
    return (firstobj, secondobj) => {
        const firstValue = firstobj[property];
        const secondValue = secondobj[property];
        return firstValue - secondValue; //升序
    };
}

猜你喜欢

转载自blog.csdn.net/qq_40861800/article/details/97909597
今日推荐