我正在尝试对对象数组进行排序,每个对象都包含一个日期
这是数组
array = {
data: [{
grievance_nature: "Nature Two"
grievance_nature_id: 19
date: "2019-01-19 06:53:20"
},
{
grievance_nature: "Nature Two"
grievance_nature_id: 19
date: "2019-01-30 06:53:20"
},
{
grievance_nature: "Nature Two"
grievance_nature_id: 19
date: "22019-01-21 19:30:32"
},
{
grievance_nature: "Nature Two"
grievance_nature_id: 19
date: "2019-01-28 06:53:20"
}
]
}
我想按日期顺序对最新数组进行排序。如果日期相同,则应按时间排序。
我尝试了这些没有给我结果的功能
array.data.sort(function(a, b) {
var c = new Date(a.date);
var d = new Date(b.date);
return c - d;
});
上述解决方案没有给我任何结果
所需的数组形式是这样:
array = {
data: [{
grievance_nature: "Nature Two"
grievance_nature_id: 19
date: "2019-01-30 06:53:20"
},
{
grievance_nature: "Nature Two"
grievance_nature_id: 19
date: "2019-01-28 06:53:20"
} {
grievance_nature: "Nature Two"
grievance_nature_id: 19
date: "22019-01-21 19:30:32"
},
{
grievance_nature: "Nature Two"
grievance_nature_id: 19
date: "2019-01-19 06:53:20"
},
]
}
答案 0 :(得分:0)