我有一个 JavaScript 数组,看起来像这样:
我希望您能按照日期顺序排序,最早的日期排在最前面,最近的日期排在最后面。 我已经尝试过。
Array[9]
0: "01/06/2016"
1: "02/06/2016"
2: "23/05/2016"
3: "24/05/2016"
4: "25/05/2016"
5: "26/05/2016"
6: "27/05/2016"
7: "28/05/2016"
8: "31/05/2016"
length: 9__proto__: Array[0]
我希望您能按照日期顺序排序,最早的日期排在最前面,最近的日期排在最后面。 我已经尝试过。
days.sort(function(a,b) {
return new Date(a).getTime() - new Date(b).getTime()
});
但我猜是因为日期的格式问题?这样不行。我还可以尝试什么?
期望的输出:
Array[9]
0: "23/05/2016"
1: "24/05/2016"
2: "25/05/2016"
3: "26/05/2016"
4: "27/05/2016"
5: "28/05/2016"
6: "31/05/2016"
7: "01/06/2016"
8: "02/06/2016"
length: 9__proto__: Array[0]