0

I have an array of dates as below:

var unavailableDates = ["31-5-2014", "1-6-2014", "2-6-2014", "3-6-2014", "26-5-2014", "27-5-2014", "28-5-2014", "29-5-2014"];

How can I sort this array without changing the date format?

Leo T Abraham
  • 2,407
  • 4
  • 28
  • 54
  • If you store them as year first, it'd be simple. Can you translate it to that format or simply change the code that returns this array? – iamdev May 20 '14 at 06:01
  • This is similar to this question: http://stackoverflow.com/questions/5002848/how-to-define-custom-sort-function-in-javascript – iamdev May 20 '14 at 06:02
  • @iamdev will it work for dates ? – Leo T Abraham May 20 '14 at 06:03
  • You would have to specify in your custom sort function (that you write) how you want it to compare 2 strings. Then it will go through using those rules and sort the list. I'm pretty sure it's the only way in your case. – iamdev May 20 '14 at 06:04
  • Have a look at : http://stackoverflow.com/questions/10123953/sort-javascript-object-array-by-date and http://stackoverflow.com/questions/7555025/jquery-fastest-way-to-sort-an-array-by-timestamp – A H K May 20 '14 at 06:18
  • You can find some useful answers to this topic here: **[Sorting objects in an array by a field value in JavaScript](http://stackoverflow.com/a/26759350/2247494)** – jherax Nov 05 '14 at 15:59

4 Answers4

2

Code speak for itself.

var unavailableDates = ["31-5-2014", "1-6-2014", "2-6-2014", "3-6-2014", "26-5-2014", "27-5-2014", "28-5-2014", "29-5-2014"];
unavailableDates = unavailableDates
    .map(function (val, idx) {
        var arr = val.split('-');
        return new Date(arr[2], arr[1] - 1, arr[0]);
    })
    .sort(function (x, y) {
        return x > y
    });
console.log(unavailableDates);

DEMO

Satpal
  • 132,252
  • 13
  • 159
  • 168
2

Hope it would be work for you, try this code

  var unavailableDates = ["31-5-2014", "1-6-2014", "2-6-2014", 
              "3-6-2014", "26-5-2014", "27-5-2014", "28-5-2014", "29-5-2014"];
    function SortByDate(a, b){
       var amyDate = a.split("-");
       var aNewDate=new Date(amyDate[1]+","+amyDate[0]+","+amyDate[2]).getTime();
       var bmyDate = b.split("-");
       var bNewDate=new Date(bmyDate[1]+","+bmyDate[0]+","+bmyDate[2]).getTime();
       return ((aNewDate < bNewDate) ? -1 : ((aNewDate > bNewDate) ? 1 : 0));
    }
    console.log(unavailableDates.sort(SortByDate));

FIDDLE DEMO

Girish
  • 11,907
  • 3
  • 34
  • 51
0

This alternative should work fine and does not change the date format at all. The sort function uses date format YYYYMMDD just for sorting only but leaves the original dates intact.

var unavailableDates = ["31-5-2014", "1-6-2014", "2-6-2014", "3-6-2014", "26-5-2014", "27-5-2014", "28-5-2014", "29-5-2014"];
unavailableDates.sort(function( a, b ) {
    var sA = a.split( '-' ).map(function(v,i) { return (i<2)?("0"+v).substr(-2):v; }).reverse().join(''); 
    var sB = b.split( '-' ).map(function(v,i) { return (i<2)?("0"+v).substr(-2):v; }).reverse().join('');
    return sA > sB;
})
console.log( unavailableDates );

JS DEMO

PeterKA
  • 24,158
  • 5
  • 26
  • 48
0
var eventDates = ["31-5-2014", "1-6-2014", "2-6-2014", "3-6-2014", "26-5-2014", "27-5-2014", "28-5-2014", "29-5-2014"];
           
var result = data.sort(function (a, b){ new Date(a.eventDates).getTime() - new Date(b.eventDates).getTime()
});

console.log(result);
  • 1
    Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. – DCCoder May 20 '21 at 17:07
  • Typo: `data.sort` should be `eventDates.sort` – Jeff Mergler May 21 '21 at 17:19