我有一个很大的对象数组,其中有一个格式为dd/mm/yyyy HH:mm:ss的字符串类型日期字段,我无法正确地对其进行重新排序。
阵列看起来像这个
cases[
{
name: a,
date:03/04/2020 17:24:17
},
{
name: b,
date:04/05/2020 07:21:19
},
{
name: c,
date:02/01/2020 11:35:50
},
]
我试着用下面的方式重新排序,但没有结果
cases.sort(function(a,b){
var dateA=new Date(a.date), dateB=new Date(b.date)
return dateA - dateB
});
试试这个
cases = [{
name: "a",
date: "03/04/2020 17:24:17"
}, {
name: "b",
date: "04/05/2020 07:21:19"
}, {
name: "c",
date: "02/01/2020 11:35:50"
}, ]
const makeDate = str => {
const [_, dd, mm, yyyy, hh, min, ss] = str.match(/(d{2})/(d{2})/(d{4}) (d{2}):(d{2}):(d{2})/);
return new Date(yyyy, mm - 1, dd, hh, min, ss)
};
cases.sort((a, b) => makeDate(a.date) - makeDate(b.date))
console.log(cases)