基于对象的日期,时间排序数组

时间:2018-08-16 09:12:37

标签: javascript

我有一个带有日期和时间的对象数组。我正在尝试对它进行排序,但是以某种方式无法正常工作。

var myArray = [
  {date: "2018-01-12T05:04:47+00:00", summary: "xyz 2 mg", readTerm: "", dosage: "Up to 20 mg daily"},
  {date: "2018-08-16T08:31:09", summary: "dax 600 mg", readTerm: "", dosage: "600 mg 3 times a day"},
  {date: "2016-04-10T05:04:47+00:00", summary: "abc 1 g", readTerm: "", dosage: "Two 1 g tabs daily at teatime"},
  {date: "2008-04-10T05:04:47+00:00", summary: "yayo 1.25 mg", readTerm: "", dosage: "1.25 mg daily each morning"},
  {date: "2016-04-10T05:04:47+00:00", summary: "jpojo 100 ug", readTerm: "", dosage: "100 ug/dose 1 to 2 puffs up to 4x daily"},
  {date: "2010-04-10T05:04:47+00:00", summary: "kolwl 15 mg", readTerm: "", dosage: "15 mg daily"},
  {date: "2014-01-12T05:04:47+00:00", summary: "kolld 200 mg", readTerm: "", dosage: "200 mg twice daily"},
  {date: "2016-04-10T05:04:47+00:00", summary: "akfd 20 mg", readTerm: "", dosage: "20 mg each morning"},
  {date: "2016-04-10T05:04:47+00:00", summary: "adfas 1000 mg", readTerm: "", dosage: "Two 500 mg tabs four times daily"},
  {date: "2016-04-10T05:04:47+00:00", summary: "afdas 20 g", readTerm: "", dosage: "20 mg daily"},
  {date: "2000-04-10T05:04:47+00:00", summary: "fdasf 75 mg", readTerm: "", dosage: "75 mg daily"},
  {date: "1990-04-10T05:04:47+00:00", summary: "dfsa 200 ug", readTerm: "", dosage: "100 ug/6 ug 2 puffs 2x daily"},
  {date: "1990-04-10T05:04:47+00:00", summary: "dsaf 125 ug", readTerm: "", dosage: "125 ug daily"}
  ]

  myArray.map((arr) => arr).sort((a,b) => new Date(a.date).getTime() < new Date(b.date).getTime());

3 个答案:

答案 0 :(得分:1)

您有一个代码错误-在arr-之后):

  arr).sort((a,b)

在github上有一个很大的答案:https://gist.github.com/onpubcom/1772996

这是另一种方式: https://onpub.com/how-to-sort-an-array-of-dates-with-javascript-s7-a109

这是您可以迭代的简单方法:

 array.sort(function(a,b){
    return new Date(b.date).getTime() - new Date(a.date).getTime();
 });

答案 1 :(得分:0)

答案 2 :(得分:0)

var myArray = [
  {date: "2018-01-12T05:04:47+00:00", summary: "xyz 2 mg", readTerm: "", dosage: "Up to 20 mg daily"},
  {date: "2018-08-16T08:31:09", summary: "dax 600 mg", readTerm: "", dosage: "600 mg 3 times a day"},
  {date: "2016-04-10T05:04:47+00:00", summary: "abc 1 g", readTerm: "", dosage: "Two 1 g tabs daily at teatime"},
  {date: "2008-04-10T05:04:47+00:00", summary: "yayo 1.25 mg", readTerm: "", dosage: "1.25 mg daily each morning"},
  {date: "2016-04-10T05:04:47+00:00", summary: "jpojo 100 ug", readTerm: "", dosage: "100 ug/dose 1 to 2 puffs up to 4x daily"},
  {date: "2010-04-10T05:04:47+00:00", summary: "kolwl 15 mg", readTerm: "", dosage: "15 mg daily"},
  {date: "2014-01-12T05:04:47+00:00", summary: "kolld 200 mg", readTerm: "", dosage: "200 mg twice daily"},
  {date: "2016-04-10T05:04:47+00:00", summary: "akfd 20 mg", readTerm: "", dosage: "20 mg each morning"},
  {date: "2016-04-10T05:04:47+00:00", summary: "adfas 1000 mg", readTerm: "", dosage: "Two 500 mg tabs four times daily"},
  {date: "2016-04-10T05:04:47+00:00", summary: "afdas 20 g", readTerm: "", dosage: "20 mg daily"},
  {date: "2000-04-10T05:04:47+00:00", summary: "fdasf 75 mg", readTerm: "", dosage: "75 mg daily"},
  {date: "1990-04-10T05:04:47+00:00", summary: "dfsa 200 ug", readTerm: "", dosage: "100 ug/6 ug 2 puffs 2x daily"},
  {date: "1990-04-10T05:04:47+00:00", summary: "dsaf 125 ug", readTerm: "", dosage: "125 ug daily"}
  ];
  
  myArray = myArray.sort((a,b) => new Date(a.date).getTime() - new Date(b.date).getTime());
  
  console.log(myArray);

如果您想按降序使用,请使用此

  myArray = myArray.sort((a,b) => new Date(b.date).getTime() - new Date(a.date).getTime());