我想把下面数组按时间排序后展示出来,使用sort方法始终无法达到期望效果,不知原因为何?求解答
let arr = [{
"applyId": "1909101016056138768202215",
"createTime": "2019-09-10 10:16:02",
"status": 0,
"userName": "李某某"
}, {
"applyId": "1909101027225522012190631",
"createTime": "2019-09-10 10:07:19",
"status": 1,
"userName": "李某某"
}, {
"applyId": "1909101027225562448906248",
"createTime": "2019-09-10 10:27:19",
"status": 3,
"userName": "李某某"
}]
function compare(p) { //这是比较函数
return function (m, n) {
var a = new Date(m[p]);
var b = new Date(n[p]);
return a > b;
}
}
arr.sort(compare("createTime"))
console.log(arr)
compare需要返回一个number类型。