Josh Kirkpatrick
Josh Kirkpatrick

Reputation: 179

Sorting array of string dates

i have an array in javascript that looks like this

Array[9]
0: "01/06/2016"
1: "02/06/2016"
2: "23/05/2016"
3: "24/05/2016"
4: "25/05/2016"
5: "26/05/2016"
6: "27/05/2016"
7: "28/05/2016"
8: "31/05/2016"
length: 9__proto__: Array[0]

i want to order them so the oldest date is first and the most recent is last. i have tried

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

but i guess because of the format of the date? this doesn't work. what else could i try?

expected output

Array[9]
0: "23/05/2016"
1: "24/05/2016"
2: "25/05/2016"
3: "26/05/2016"
4: "27/05/2016"
5: "28/05/2016"
6: "31/05/2016"
7: "01/06/2016"
8: "02/06/2016"
length: 9__proto__: Array[0]

Upvotes: 3

Views: 132

Answers (5)

Edward Pham
Edward Pham

Reputation: 83

Please try this:

var array = ["01/06/2016", "02/06/2016", "23/05/2016", "24/05/2016", "25/05/2016", "26/05/2016", "27/05/2016", "28/05/2016", "31/05/2016"];
    function dateString2Date(dateString) {
      var dt  = dateString.split(/\//);
      return new Date(dt[2]+"-"+dt[1]+"-"+dt[0]);
    }
    for(var i =0 ; i<=array.length-2;i++){
        for(var j=i+1; j<=array.length-1;j++){
            if(dateString2Date((array[i])).getTime()/1000> dateString2Date((array[j])).getTime()/1000){
                var temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }

console.log(array)

Upvotes: 0

Thomas
Thomas

Reputation: 3593

please separate the parsing and the sorting-operations and cache the intermediate-values.

days = ["01/06/2016", "02/06/2016", "23/05/2016", "24/05/2016", "25/05/2016", "26/05/2016", "27/05/2016", "28/05/2016", "31/05/2016"];


days.map(v => {  //parsing
  var a = v.split("/");
  return {
    value: v,
    ts: +new Date(+a[2], a[1]-1, +a[0])
  }
})
.sort((a,b) => a.ts - b.ts)  //sorting
.map(o => o.value);  //returning the associated (input-)values

For n items in the input-array, the sorting-function may be called up to n * (n-1) times, depending on the implemented sorting-algorithm.
That's in this case, up to 144 times parsing such a string, in the worst case.

In the very best case (array already sorted) the other implementations here have to parse at least 16 times for this 9 items. (8 comparisons * 2 strings to parse)

This may not sound like much (yet), but these numbers increase exponentially.

Upvotes: 0

Alper Şaldırak
Alper Şaldırak

Reputation: 1054

var array= [ "01/06/2016" ,
 "02/06/2016" ,
 "23/05/2016" ,
 "24/05/2016" ,
 "25/05/2016" ,
 "26/05/2016" ,
 "27/05/2016" ,
 "28/05/2016" ,
 "31/05/2016" ];
 array.sort(function (a, b) {
 var dateParts1 = a.split("/");
 var dateParts2 = b.split("/");
 var dateA=dateParts1[2]*360+ dateParts1[1]*30+ dateParts1[0];
 var dateB=dateParts2[2]*360+ dateParts2[1]*30+ dateParts2[0];
  if (dateA > dateB) {
    return 1;
  }
  if (dateA < dateB) {
    return -1;
  }
  return 0;
});

Upvotes: 0

rrk
rrk

Reputation: 15846

You can slit the string to year, month, date and use that to create the date for comparing.

new Date("01/06/2016") is wont be parsed as you think. The result actually is Jan 06 2016.

days = ["01/06/2016", "02/06/2016", "23/05/2016", "24/05/2016", "25/05/2016", "26/05/2016", "27/05/2016", "28/05/2016", "31/05/2016"];
days.sort(function(a, b) {
  aArr = a.split('/');
  bArr = b.split('/');
  return new Date(aArr[2], Number(aArr[1])-1, aArr[0]).getTime() - new Date(bArr[2], Number(bArr[1])-1, bArr[0]).getTime()
});
console.log(days);

Upvotes: 4

M_T
M_T

Reputation: 397

It's because the format Date uses in your case is MM/DD/YYYY

new Date("01/06/2016");
> Wed Jan 06 2016 00:00:00 GMT+0100 (Mitteleuropäische Zeit) 

See also http://www.w3schools.com/js/js_date_formats.asp

But from your list i assume your dates are in DD/MM/YYYY format.

3 possible solutions:

  • use a different format in your list

  • You could split your string up and create the Date via new Date(year, month, day); in your sort function.

  • use an advanced date/time library, i recommend moment.js http://momentjs.com/

Upvotes: 3

Related Questions