按日期排序对象数组

时间:2018-04-29 23:26:25

标签: javascript arrays

这是我返回后传递给数组的JSON文件的结构。我试图按日期排序。

var model = [ {id: "1", date: "2018-05-08", racedistance: "10000", race_name: "Riverside Park", venue_name: "Riverside Park", …},
{id: "140", date: "2018-05-01", racedistance: "42195", race_name: "Placeholder Race Name 1", venue_name: "Riverside Park", …},
{id: "149", date: "2018-05-10", racedistance: "10000", race_name: "Placeholder Race Name 3", venue_name: "Riverside Park", …},
{id: "172", date: "2018-06-04", racedistance: "21097", race_name: "Placeholder Race Name 3", venue_name: "Riverside Park", …},
{id: "177", date: "2018-06-06", racedistance: "21097", race_name: "Placeholder Race Name 5", venue_name: "Riverside Park", …},
{id: "178", date: "2018-06-10", racedistance: "21097", race_name: "Placeholder Race Name 1", venue_name: "Riverside Park", …},
{id: "183", date: "2018-06-13", racedistance: "10000", race_name: "Placeholder Race Name 2", venue_name: "Riverside Park", …},
{id: "198", date: "2018-06-27", racedistance: "21097", race_name: "Placeholder Race Name 2", venue_name: "Riverside Park", …},
{id: "144", date: "2018-05-04", racedistance: "42195", race_name: "Placeholder Race Name 5", venue_name: "Albert Park", …}]

//这是我一直试图使用的排序函数。

function mycomparator(a,b) {
  return new Date(a.date, 10) - new Date(b.date, 10);
}
model.sort(mycomparator);

//这是返回数组的方式。

{id: "1", date: "2018-05-08", racedistance: "10000", race_name: "Riverside Park", venue_name: "Riverside Park", …},
{id: "192", date: "2018-06-23", racedistance: "10000", race_name: "Placeholder Race Name 2", venue_name: "Saltwell Park to Chopwell Woods", …},
{id: "149", date: "2018-05-10", racedistance: "10000", race_name: "Placeholder Race Name 3", venue_name: "Riverside Park", …},
{id: "172", date: "2018-06-04", racedistance: "21097", race_name: "Placeholder Race Name 3", venue_name: "Riverside Park", …},
{id: "177", date: "2018-06-06", racedistance: "21097", race_name: "Placeholder Race Name 5", venue_name: "Riverside Park", …},
{id: "178", date: "2018-06-10", racedistance: "21097", race_name: "Placeholder Race Name 1", venue_name: "Riverside Park", …},
{id: "183", date: "2018-06-13", racedistance: "10000", race_name: "Placeholder Race Name 2", venue_name: "Riverside Park", …}

我想按日期排序,最早的第一个。

1 个答案:

答案 0 :(得分:-1)

var model = [ {id: "1", date: "2018-05-08", racedistance: "10000", race_name: "Riverside Park", venue_name: "Riverside Park"},
{id: "140", date: "2018-05-01", racedistance: "42195", race_name: "Placeholder Race Name 1", venue_name: "Riverside Park"},
{id: "149", date: "2018-05-10", racedistance: "10000", race_name: "Placeholder Race Name 3", venue_name: "Riverside Park"},
{id: "172", date: "2018-06-04", racedistance: "21097", race_name: "Placeholder Race Name 3", venue_name: "Riverside Park"},
{id: "177", date: "2018-06-06", racedistance: "21097", race_name: "Placeholder Race Name 5", venue_name: "Riverside Park"},
{id: "178", date: "2018-06-10", racedistance: "21097", race_name: "Placeholder Race Name 1", venue_name: "Riverside Park"},
{id: "183", date: "2018-06-13", racedistance: "10000", race_name: "Placeholder Race Name 2", venue_name: "Riverside Park"},
{id: "198", date: "2018-06-27", racedistance: "21097", race_name: "Placeholder Race Name 2", venue_name: "Riverside Park"},
{id: "144", date: "2018-05-04", racedistance: "42195", race_name: "Placeholder Race Name 5", venue_name: "Albert Park"}];



model.sort(function(a, b) {
    a = new Date(a.date);
    b = new Date(b.date);
    return a<b ? -1 : a>b ? 1 : 0;
});

for(var key in model){
	console.log(model[key].date);
}