朱丽叶

导航

数组根据时间戳排序

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');

posted on 2022-08-14 19:33  朱丽叶  阅读(139)  评论(0)    收藏  举报