首页 > 解决方案 > 通过一个函数中的多个比较对对象数组进行排序

问题描述

我有一个对象数组:

const arr = [
  {
    name: 'Kevin',
    timestamp: "1617834779855"
  },
  {
    name: 'Louis',
    timestamp: "1617334735485"
  },
  {
    name: 'Adisson',
    timestamp: "1616530999700"
  },
]

我需要按最近的时间戳和名称按字母顺序排序。现在我有一个按日期排序的排序函数,但我不知道如何将名称添加到排序函数中

const i = arr.sort((a, b) => b.timestamp - a.timestamp);

我的第一个想法是添加另一个排序函数,但我不希望它再次遍历数组,因为在我的应用程序中,数组的长度超过 1000 个

标签: javascript

解决方案


推荐阅读