我有一个包含日期和其他一些数据的数组。我想使用日期对这个数组进行排序。我该怎么做?我不知道。请帮帮我。.
这是我的数组 =>
0:{_id: "01-11-2017", CommentCount: 221, Likecount: 141, Followcount: 0, Unfollowcount: 116}
1:{_id: "27-10-2017", CommentCount: 235, Likecount: 495, Followcount: 802, Unfollowcount: 98}
2:{_id: "28-10-2017", CommentCount: 232, Likecount: 52, Followcount: 0, Unfollowcount: 799}
3:{_id: "29-10-2017", CommentCount: 236, Likecount: 52, Followcount: 0, Unfollowcount: 611}
4:{_id: "30-10-2017", CommentCount: 233, Likecount: 58, Followcount: 0, Unfollowcount: 0}
5:{_id: "31-10-2017", CommentCount: 240, Likecount: 175, Followcount: 0, Unfollowcount: 0}
预期输出 =>
0:{_id: "27-10-2017", CommentCount: 235, Likecount: 495, Followcount: 802, Unfollowcount: 98}
1:{_id: "28-10-2017", CommentCount: 232, Likecount: 52, Followcount: 0, Unfollowcount: 799}
2:{_id: "29-10-2017", CommentCount: 236, Likecount: 52, Followcount: 0, Unfollowcount: 611}
3:{_id: "30-10-2017", CommentCount: 233, Likecount: 58, Followcount: 0, Unfollowcount: 0}
4:{_id: "31-10-2017", CommentCount: 240, Likecount: 175, Followcount: 0, Unfollowcount: 0}
5:{_id: "01-11-2017", CommentCount: 221, Likecount: 141, Followcount: 0, Unfollowcount: 116}
JavaScript 不支持您的日期格式,因此您必须在将其发送到日期构造函数之前将其反转,然后再进行比较
console.log(arr.sort(function(a,b) {
return new Date(a._id.split('-').reverse().join('-')).getTime() - new Date(b._id.split('-').reverse().join('-')).getTime()
}))
这将解决问题
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句