1

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]);
});
Rakesh Kumar
  • 2,705
  • 1
  • 19
  • 33
  • For starters, `a` is an array like `[ '11/15/2013', 0.189 ]` so `a[0][0]` would result in `'1'`.. This still requires work, however because `new Date(a[0])` - eg `new Date('11/15/2013')` - is ill-defined behavior. I recommend [Moment.js](http://momentjs.com/) to deal with this situation, but one can manually parse the date and use `new Date(y, m, d)` as well: there are no shortages of such examples when asking how to turn a string into a Date – user2864740 Aug 14 '15 at 07:46
  • For how to *"properly"* create a Date, see http://stackoverflow.com/questions/5619202/converting-string-to-date-in-js , http://stackoverflow.com/questions/22184747/parse-string-to-date-with-moment-js , etc – user2864740 Aug 14 '15 at 07:51

3 Answers3

7

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 ] ]
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • FWIW, see my comment on the main post. Passing in a locale-specific string to the Date constructor ill-defined behavior. – user2864740 Aug 14 '15 at 07:50
1

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>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

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]);});
Dedy Chaidir
  • 767
  • 6
  • 15