js根据时间做升序排列(数组内)

2025年01月16日 建站教程

const arr = [
  {id: 1, value : "value1", date: "2023-04-08", time: "13:11:17"},
  {id: 2, value : "value2", date: "2023-04-09", time: "11:11:17"},
  {id: 3, value : "value3", date: "2023-04-10", time: "17:11:17"},
  {id: 4, value : "value4", date: "2023-04-10", time: "08:11:17"},
  {id: 5, value : "value5", date: "2023-04-10", time: "12:11:17"},
  {id: 6, value : "value6", date: "2023-04-10", time: "22:11:17"},
  {id: 7, value : "value7", date: "2023-04-10", time: "17:11:17"},
  {id: 8, value : "value8", date: "2023-04-11", time: "22:11:17"}
];

arr.sort((a, b) => b.date.localeCompare(a.date) || b.time.localeCompare(a.time));
console.log(arr);
//输出结果:
0: {id: 8, value: 'value8', date: '2023-04-11', time: '22:11:17'}
1: {id: 6, value: 'value6', date: '2023-04-10', time: '22:11:17'}
2: {id: 3, value: 'value3', date: '2023-04-10', time: '17:11:17'}
3: {id: 7, value: 'value7', date: '2023-04-10', time: '17:11:17'}
4: {id: 5, value: 'value5', date: '2023-04-10', time: '12:11:17'}
5: {id: 4, value: 'value4', date: '2023-04-10', time: '08:11:17'}
6: {id: 2, value: 'value2', date: '2023-04-09', time: '11:11:17'}
7: {id: 1, value: 'value1', date: '2023-04-08', time: '13:11:17'}

本文链接:http://so.lmcjl.com/news/21448/

展开阅读全文
相关内容