-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?

SE_net4 the downvoter
  • 21,043
  • 11
  • 69
  • 107
Sam B.
  • 1,783
  • 2
  • 24
  • 57

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.
  • 1,783
  • 2
  • 24
  • 57