4

For some strange reason the following function I wrote for converting an array of date strings to javascript date objects, sorting them, then returning an array of sorted date strings does not sort properly:

sortdates: function(dates, separator) {
    var sorteddates = [],
        datestr =[];
    sorteddates = dates.map(function(val) {
        return new Date(val.replace("/"+separator+"/g", " "));
    }).sort();
    for ( i=0; i<sorteddates.length; i++ ) {
        datestr.push((sorteddates[i].getMonth()+1) + "-" + sorteddates[i].getDate() + "-" + sorteddates[i].getFullYear());
    }
    return datestr;
}

If I make a test array of date strings and apply this function:

var testarray = ["2013-8-1", "2013-8-8", "2013-8-15", "2013-8-22", "2013-9-5", "2013-9-12", "2013-8-2", "2013-8-3", "2013-8-4", "2013-8-7", "2013-8-11", "2013-8-14", "2013-8-18", "2013-8-25"];
console.log(sortdates(testarray, "-"));

I get the following logged to the console:

["8-2-2013", "8-3-2013", "8-4-2013", "8-11-2013", "8-18-2013", "8-25-2013", "8-1-2013", "8-8-2013", "8-15-2013", "8-22-2013", "9-5-2013", "9-12-2013", "8-7-2013", "8-14-2013"]

Clearly not sorted.

0

2 Answers 2

0

.sort() will sort in "dictionary" order, not numeric or date/time order. If the values aren't strings they'll get a default string conversion which for dates does not give you something useful to sort on.

But you can supply a custom sort callback that knows how to sort dates:

.sort(function(a,b) { return a.getTime() - b.getTime(); });

Demo: http://jsfiddle.net/TUAz9/

Sign up to request clarification or add additional context in comments.

Comments

0

sort() will sort based on the string representation of the entries in the array. In this case, the string representation of a Date is something like Thu Aug 01 2013 00:00:00 GMT-0700 (PDT), so the primary sort will be by the day of the week, alphabetically.

Fri Aug 02 2013 00:00:00 GMT-0700 (PDT)
Sat Aug 03 2013 00:00:00 GMT-0700 (PDT)
Sun Aug 04 2013 00:00:00 GMT-0700 (PDT)
Sun Aug 11 2013 00:00:00 GMT-0700 (PDT)
Sun Aug 18 2013 00:00:00 GMT-0700 (PDT)
Sun Aug 25 2013 00:00:00 GMT-0700 (PDT)
Thu Aug 01 2013 00:00:00 GMT-0700 (PDT)
Thu Aug 08 2013 00:00:00 GMT-0700 (PDT)
Thu Aug 15 2013 00:00:00 GMT-0700 (PDT)
Thu Aug 22 2013 00:00:00 GMT-0700 (PDT)
Thu Sep 05 2013 00:00:00 GMT-0700 (PDT)
Thu Sep 12 2013 00:00:00 GMT-0700 (PDT)
Wed Aug 07 2013 00:00:00 GMT-0700 (PDT)
Wed Aug 14 2013 00:00:00 GMT-0700 (PDT)

You probably want to look at using a custom sort callback function, or some other representation of the date - perhaps start by mapping the array to a timestamp value instead of a Date object?

return new Date(/* stuff */).getTime();

...along with the corresponding changes to the formatting code later in your function.

Putting this all together, a function like this would work like you're expecting:

sortdates: function(dates, separator) {
    return dates.map(function(val) {
        return new Date(val.replace("/"+separator+"/g", " ")).getTime();
    }).sort().map(function(val) {
        var d = new Date(val);
        return (d.getMonth()+1) + "-" + d.getDate() + "-" + d.getFullYear();
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.