首页 > 解决方案 > 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)]

我现在需要将它们从最早的日期排序到最旧/最新的日期。建议的问题并不完全适用于这些场景,因为与包含日期的数组的列表不同,这纯粹是一个日期数组。

我怎么做?

标签: 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().sort(function(a, b){
        return a - b;
        });;
}

推荐阅读