-2

I have this function that returns me an array of date objects.

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)]

I now need to sort them from the earliest date to the oldest/latest date. The suggested question doesn't quite apply to these scenario since unlike a list with an array containing a date, this is purely an array of dates.

How do I do that?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Sam B.
  • 2,703
  • 8
  • 40
  • 78

1 Answers1

0

This is the solution I found

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;
        });;
}
Sam B.
  • 2,703
  • 8
  • 40
  • 78