简体   繁体   English

如何按多个日期字段对对象数组进行排序?

[英]How to sort an array of objects by multiple date fields?

I have the following data structure:我有以下数据结构:

 var dates = [ { id: '1', date1: '2022-03-21T18:59:36.641Z', date2: '2022-03-17T18:59:36.641Z', }, { id: '2', date1: '2022-03-20T18:59:36.641Z', date2: '2022-03-17T18:59:36.641Z', }, { id: '3', date2: '2022-03-17T18:59:36.641Z', }, { id: '4', date2: '2022-03-15T18:59:36.641Z', } ]; var sorted = dates.sort(function(a,b) { return (a.date1 > b.date1)? 1: -1 }); console.log({sorted});

Notice that date1 is not always available, but date2 is required.请注意,date1 并不总是可用,但 date2 是必需的。 I'd like to sort by date1 first, then date2.我想先按 date1 排序,然后按 date2 排序。 I've created this fiddle to test, but still haven't figured it out: https://jsfiddle.net/y9sgpob8/4/我创建了这个小提琴来测试,但仍然没有弄明白: https://jsfiddle.net/y9sgpob8/4/

Please help me figure out how to get the results in the following order:请帮我弄清楚如何按以下顺序获得结果:

[{
  date1: "2022-03-20T18:59:36.641Z",
  date2: "2022-03-17T18:59:36.641Z",
  id: "2"
}, {
  date1: "2022-03-21T18:59:36.641Z",
  date2: "2022-03-17T18:59:36.641Z",
  id: "1"
}, {
  date2: "2022-03-15T18:59:36.641Z",
  id: "4"
}, {
  date2: "2022-03-17T18:59:36.641Z",
  id: "3"
}]

You need to sort based on你需要根据

  • whether date1 exists date1是否存在
  • then based on date1 value然后根据date1
  • then based on date2 value然后根据date2

Since the dates are in ISO format, you can do string comparison to sort由于日期是ISO格式,你可以做字符串比较来排序

 const input=[{id:"1",date1:"2022-03-21T18:59:36.641Z",date2:"2022-03-17T18:59:36.641Z",},{id:"2",date1:"2022-03-20T18:59:36.641Z",date2:"2022-03-17T18:59:36.641Z",},{id:"3",date2:"2022-03-17T18:59:36.641Z",},{id:"4",date2:"2022-03-15T18:59:36.641Z",}]; input.sort((a,b) => ( ('date1' in b) - ('date1' in a) ) || (a.date1?? '').localeCompare(b.date1?? '') || a.date2.localeCompare(b.date2) ) console.log(input)

You could also separate the objects that only have date2 then sort and lastly merge it.您还可以分离只有date2的对象,然后排序并最后合并它。 See snippet below:请看下面的片段:

 const dates = [ { id: '1', date1: '2022-03-21T18:59:36.641Z', date2: '2022-03-17T18:59:36.641Z', }, { id: '2', date1: '2022-03-20T18:59:36.641Z', date2: '2022-03-17T18:59:36.641Z', }, { id: '3', date2: '2022-03-17T18:59:36.641Z', }, { id: '4', date2: '2022-03-15T18:59:36.641Z', } ]; var array1 = []; var array2 = []; for (const date in dates) { if (dates[date].date1) { array1.push(dates[date]); } else { array2.push(dates[date]); } } var sorted1 = array1.sort(function(a,b) { return (a.date1 > b.date1)? 1: -1 }); var sorted2 = array2.sort(function(a,b) { return (a.date2 > b.date2)? 1: -1 }); const sorted = sorted1.concat(sorted2); console.log({sorted});

All records not have date1 will be found in the end of the result, right?所有没有date1的记录都会在结果的末尾找到,对吧? If so, You can separate to 2 arrays and merge them.如果是这样,您可以分离到 2 arrays 并将它们合并。

 const dates=[{id:"1",date1:"2022-03-21T18:59:36.641Z",date2:"2022-03-17T18:59:36.641Z",},{id:"2",date1:"2022-03-20T18:59:36.641Z",date2:"2022-03-17T18:59:36.641Z",},{id:"3",date2:"2022-03-17T18:59:36.641Z",},{id:"4",date2:"2022-03-15T18:59:36.641Z",}]; const timeStamp = value => new Date(value).valueOf() const arrWithDate1 = dates.filter(elem => elem.date1).sort((a, b) => (timeStamp(a.date1) - timeStamp(b.date1))) const arrWithDate2 = dates.filter(elem =>.elem.date1),sort((a. b) => (timeStamp(a.date2) - timeStamp(b.date2))) console.log([..,arrWithDate1. ..;arrWithDate2]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM