Sort dates not returned Sort dates

For some strange reason, the following function that I wrote to convert an array of date strings to javascript date objects, sort them, and then return an array of sorted date strings is not sorted 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 create 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 entries 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.

+4
source share
2 answers

.sort() will be sorted in dictionary order, not digitally or by date / time. If the values ​​are not strings, they will get the default string conversion, which for dates does not give you anything useful for sorting.

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

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

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

+1
source

sort() will be sorted based on the string representation of the entries in the array. In this case, the string representation of the date looks like Thu Aug 01 2013 00:00:00 GMT-0700 (PDT) , so the main view will be in alphabetical order by day of the week.

 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'll probably want to take a look at using a custom sort callback function or some other date representation β€” maybe start by matching the array with the timestamp value instead of the Date object?

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

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

Combining all this, such a function will work as you expect:

 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(); }); } 
+1
source

Source: https://habr.com/ru/post/1495165/


All Articles