经常会遇到需要给日期排序,比如下面的数组:
var=[
{ name: "A", data: "2017-08-17 13:56:14", },
{ name: "B", data: "2017-08-18 20:45:44", },
{ name: "C", data: "2017-08-19 05:12:21", },
{ name: "D", data: "2017-07-21 13:23:32", },
{ name: "E", data: "2017-08-18 12:24:54", },
{ name: "F", data: "2017-08-30 10:29:02", }
]
实现排序
function sortDownDate(a, b) {
return Date.parse(a.data) - Date.parse(b.data);
}
function sortUpDate(a, b) {
return Date.parse(b.data) - Date.parse(a.data);
}
//console.log(arr.sort(sortDownDate));//正序
//console.log(arr.sort(sortUpDate));//反序