export function compare(arr, key, type = "asc") {
return arr.sort((value1, value2) => {
const val1 = value1[key];
const val2 = value2[key];
// return val2-val1 降序排列,return val1-val2; 升序
if (type === 'asc') {
return val2 - val1
} else {
return val1 - val2
}
});
}
// 使用
const listData = [
{ id:1,"created": 1658312753000,},
{ id:2,"created": 1658219841000,},
{ id:3,"created": 1659092822000,},
{ id:4,"created": 1658365705000,},
{ id:5,"created": 1658320951000,},
]
import {compare} from "@/utils/utils.js";
const arr = compare(listData,'created');
标签:return,created,根据,数组,val2,val1,排序,id,const
From: https://www.cnblogs.com/rzl795/p/16586122.html