0

I have a JavaScritpt Date sorting question. I have an array of up to 24 dates that I need to sort in descending order. The dates were formatted in a full date format. I successfully converted to a mm/dd/yyyy. I realized that I cannot use sort and I believe I need to run a comparisons of sort.

My dates appear as below

eventDatesArrray = ,11/30/2014,12/31/2018,12/31/2013,10/11/2016,10/12/2015

I did reference Sort Javascript Object Array By Date. My apologies, but I do not believe I completely understand how it works since the I continue to receive an error message stating that I am not passing the parameters. Not certain how to pass a and b to this function.

var sortedEventDates = eventDatesArrray .sort(function(a, b) {
    return a>b ? -1 : a<b ? 1 : 0;
});

Thank you in advance for your help. Allison

Community
  • 1
  • 1
user3788942
  • 73
  • 1
  • 2
  • 8

3 Answers3

1

If dates are strings:

dates = ["10/12/2014", "12/12/2015", "12/31/2015", "14/08/2015"]

dates.sort(function(a, b) {
    var parseDate = function parseDate(dateAsString) {
            var dateParts = dateAsString.split("/");
            return new Date(parseInt(dateParts[2], 10), parseInt(dateParts[1], 10) - 1, parseInt(dateParts[0], 10));
        };

    return parseDate(b) - parseDate(a);
});

["12/31/2015", "12/12/2015", "14/08/2015", "10/12/2014"]

  • 1
    You don't need to use *parseInt*, `new Date(dateParts[2], dateParts[1] - 1, dateParts[0])` is just fine as the Date constructor accepts string values too. You can also simply do: `return parsedDateA - parsedDateB` since the subtraction operator will coerce the Dates to number (their internal time value). :-) Oh, and `parsedDateA === parsedDateB` will never be true since Dates are Objects, and Objects are never equal to each other (whether using `==` or `===`). – RobG Aug 15 '16 at 00:45
0

Or in ES6 :

var eventDatesArrray = ["11/30/2014","12/31/2018","12/31/2013","10/11/2016","10/12/2015"];
var dates = eventDatesArrray.map(elem => new Date(elem)).sort((a,b) => a-b);
console.log(dates); //[ Tue Dec 31 2013 00:00:00 GMT+0100 (Paris, Madrid),
                    //  Sun Nov 30 2014 00:00:00 GMT+0100 (Paris, Madrid),
                    //  Mon Oct 12 2015 00:00:00 GMT+0200 (Paris, Madrid (heure d’été)),
                    //  Tue Oct 11 2016 00:00:00 GMT+0200 (Paris, Madrid (heure d’été)),
                    //  Mon Dec 31 2018 00:00:00 GMT+0100 (Paris, Madrid) ]
kevin ternet
  • 3,779
  • 2
  • 14
  • 23
  • Parsing strings with the Date constructor (or Date.parse, they are equivalent for parsing) is not recommended as it's almost entirely implementation dependent. Manually parse strings with a library or bespoke function (per Adnrzej's answer). – RobG Aug 15 '16 at 00:51
0

You could use this as a custom comparator function:

function compare (a, b) {
    partsA = a.split("/");
    partsB = b.split("/");
    if (partsA[2] > partsB[2]) {
        return -1;
    } else if (partsA[2] < partsB[2]) {
        return 1;
    } else {
        if (partsA[1] > partsB[1]) {
            return -1;
        } else if (partsA[1] < partsB[1]) {
            return 1;
        } else {
            if (partsA[0] > partsB[0]) {
                return -1;
            } else if (partsA[0] < partsB[0]) {
                return 1;
            } else {
                return 0;
            }
        }
    }
}

Then call eventDatesArray.sort(compare);