Javascript对日期对象数组进行排序

时间:2019-09-26 13:37:59

标签: javascript

我有这个函数,它返回一个日期对象数组。

function get_location_dates(filter){
    return filter.map(function(){
            var [day, month, year] = $(this).text().split('-');
            return new Date(parseInt(year), parseInt(month)-1, parseInt(day));
    }).get();
}

$('#dropoff-locations .date:first').text();
"25-09-2019"

var dropoff =  $('#dropoff-locations .date');
undefined
get_location_dates(dropoff);
(2) [Wed Sep 25 2019 00:00:00 GMT+0300 (East Africa Time), Tue Sep 24 2019 00:00:00 GMT+0300 (East Africa Time)]

我现在需要从最早的日期到最早的/最新的日期对它们进行排序。 suggested question不适用于这些情况,因为与包含日期数组的列表不同,这纯粹是日期数组。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

这是我找到的解决方法

function get_location_dates(filter){
    return filter.map(function(){
            var [day, month, year] = $(this).text().split('-');
            return new Date(parseInt(year), parseInt(month)-1, parseInt(day));
    }).get().sort(function(a, b){
        return a - b;
        });;
}