Rakesh Kumar
Rakesh Kumar

Reputation: 2853

How to sort date array in javascript

I have below array of dates and I want to sort it in acceding order. I tried but not getting the positive result.

var arr = [ [ '02/13/2015', 0.096 ],
  [ '11/15/2013', 0.189 ],
  [ '05/15/2014', 0.11 ],
  [ '12/13/2013', 0.1285 ],
  [ '01/15/2013', 0.12 ],
  [ '01/15/2014', 0.11 ],
  [ '02/14/2014', 0.11 ],
  [ '03/14/2014', 0.11 ],
  [ '01/15/2015', 0.096 ],
  [ '07/15/2015', 0.096 ],
  [ '04/15/2013', 0.12 ],
  [ '04/15/2014', 0.11 ],
  [ '05/15/2013', 0.12 ],
  [ '06/14/2013', 0.12 ],
  [ '06/16/2014', 0.11 ],
  [ '07/15/2013', 0.12 ],
  [ '07/15/2014', 0.11 ],
  [ '03/16/2015', 0.096 ]]

My Code

arr.sort(function(a,b){
  return new Date(a[0][0]) - new Date(b[0][0]);
});

Upvotes: 1

Views: 127

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386578

Heavily inspired from MDN Sorting with map. It is important for larger data sets.

var arr = [
    ['02/13/2015', 0.096],
    ['11/15/2013', 0.189],
    ['05/15/2014', 0.11],
    ['12/13/2013', 0.1285],
    ['01/15/2013', 0.12],
    ['01/15/2014', 0.11],
    ['02/14/2014', 0.11],
    ['03/14/2014', 0.11],
    ['01/15/2015', 0.096],
    ['07/15/2015', 0.096],
    ['04/15/2013', 0.12],
    ['04/15/2014', 0.11],
    ['05/15/2013', 0.12],
    ['06/14/2013', 0.12],
    ['06/16/2014', 0.11],
    ['07/15/2013', 0.12],
    ['07/15/2014', 0.11],
    ['03/16/2015', 0.096]
];

// temporary array holds objects with position and sort-value
var mapped = arr.map(function (el, i) {
    var d = el[0].split('/');
    return { index: i, value: new Date(d[2], d[0] - 1, d[1]) };
})

// sorting the mapped array containing the reduced values
mapped.sort(function (a, b) {
    return +(a.value > b.value) || +(a.value === b.value) - 1;
});

// container for the resulting order
var result = mapped.map(function (el) {
    return arr[el.index];
});

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

Upvotes: 1

Dedy Chaidir
Dedy Chaidir

Reputation: 927

If what you want is sorting only by the dates, I think this code would help:

arr.sort(function(a,b){return new Date(a[0]) - new Date(b[0]);});

Upvotes: 0

thefourtheye
thefourtheye

Reputation: 239473

You are taking the first character in the date strings, converting them to Date instances and using them for comparison.

But, you should actually use the Date strings as they are, convert them to Date instances and compare them.

arr.sort(function (a, b) {
  return new Date(a[0]) - new Date(b[0]);
});

Output

[ [ '01/15/2013', 0.12 ],
  [ '04/15/2013', 0.12 ],
  [ '05/15/2013', 0.12 ],
  [ '06/14/2013', 0.12 ],
  [ '07/15/2013', 0.12 ],
  [ '11/15/2013', 0.189 ],
  [ '12/13/2013', 0.1285 ],
  [ '01/15/2014', 0.11 ],
  [ '02/14/2014', 0.11 ],
  [ '03/14/2014', 0.11 ],
  [ '04/15/2014', 0.11 ],
  [ '05/15/2014', 0.11 ],
  [ '06/16/2014', 0.11 ],
  [ '07/15/2014', 0.11 ],
  [ '01/15/2015', 0.096 ],
  [ '02/13/2015', 0.096 ],
  [ '03/16/2015', 0.096 ],
  [ '07/15/2015', 0.096 ] ]

Upvotes: 7

Related Questions