根据创建日期排序:
const compare = (property) => {
return function (a, b) {
var value1 = a[property];
var value2 = b[property];
return value2 > value1 ? -1 : 1;
};
};
var arr = [
{
id: 4,
gmtCreate: "2023/04/15 10:53:30",
},
{
id: 1,
gmtCreate: "2023/03/18 09:53:30",
},
{
id: 2,
gmtCreate: "2023/04/19 09:53:30",
},
{
id: 3,
gmtCreate: "2023/05/19 09:55:30",
},
];
console.log(arr.sort((a,b)=>{
return a.gmtCreate > b.gmtCreate ? 1 : -1;
}));
//或
console.log(arr.sort(compare('id')))
打印结果