0

I want to sort an array with pairs, where the first item in each pair is a date and the second an integer. I want to sort it on the dates and I'm stuck on how to do it.

example array

var dataTable = [[Fri Jun 13 2014 17:44:25 GMT+0200 (W. Europe Daylight Time),4],[Tue May 27 2014 06:56:06 GMT+0200 (W. Europe Daylight Time),4]]

dataTable.sort(function(a,b) { return  a[0]<b[0]});//How to sort the arraylist on the dates.

Thanks in advance.

anders
  • 1,465
  • 5
  • 19
  • 38

2 Answers2

2

You need a small function to convert the date strings to Date objects:

// Create a Date object from a date string in the format Tue May 27 2014 06:56:06
function parseDate(d) {
  var months = {jan:0,feb:1,mar:2,apr:3,may:4,jun:5,
                jul:6,aug:7,sep:8,oct:9,nov:10,dec:11};
  var b = d.split(/[ :]/);
  return new Date(b[3], months[b[1].toLowerCase()], b[2], b[4], b[5], b[6]);
}

Then you can use date objects in the sort function:

var dataTable = [
  ['Fri Jun 13 2014 17:44:25 GMT+0200 (W. Europe Daylight Time)',4],
  ['Tue May 27 2014 06:56:06 GMT+0200 (W. Europe Daylight Time)',4]
];

dataTable.sort(function(a, b){
  return parseDate(a[0]) - parseDate(b[0])
});

Note that calling the Date function as a constructor with a string argument (see ECMAScript §15.9.3.2) is the same as calling Date.parse and that parsing of date strings is largely implementation dependent and unreliable, even when using the format specified in ECMA-262 in browsers that support it.

RobG
  • 124,520
  • 28
  • 153
  • 188
  • A simple - return new Date(a[0]).getTime() > new Date(b[0]).getTime(); works, no need of conversion and all – Chakravarthy S M Aug 05 '14 at 12:20
  • 1
    The compare function used in [*Array.prototype.sort*](http://ecma-international.org/ecma-262/5.1/#sec-15.4.4.11) should return 1, 0 or -1 depending on the result of the comparison. Using ` – RobG Aug 05 '14 at 12:28
0

This should work

     var dataTable = [
    ["Fri Jun 13 2014 17: 44: 25 GMT + 0200(W.Europe Daylight Time)", 4],
    ["Tue May 27 2014 06: 56: 06 GMT + 0200(W.Europe Daylight Time)", 4],
    ["Fri August 13 2014 17: 44: 25 GMT + 0200(W.Europe Daylight Time)", 4],
    ["Tue October 27 2014 06: 56: 06 GMT + 0200(W.Europe Daylight Time)", 4],
    ["Tue February 27 2014 06: 56: 06 GMT + 0200(W.Europe Daylight Time)", 4]
];


dataTable.sort(function (a, b) {
    return new Date(a[0]).getTime() - new Date(b[0]).getTime();
});
console.log(dataTable);

Check the Working Fiddle Here

Chakravarthy S M
  • 590
  • 5
  • 14
  • 1
    `sort` should return -1, 0, or 1 -- not a bool. – Casey Falk Aug 05 '14 at 12:04
  • That sorts alphabetically. You need `new Date(a[0]) - new Date(b[0])` or something. – soktinpk Aug 05 '14 at 12:05
  • @ChakravarthySM—using [*Date.parse*](http://ecma-international.org/ecma-262/5.1/#sec-15.9.4.2) to parse date strings is notoriously inconsistent between browsers, especially when non–standard strings are involved. And returning true or false from the compare function will not sort correctly, it is effectively only returning 0 or 1, never -1. – RobG Aug 05 '14 at 12:20
  • It is a consequence of using `new Date(string)`, see [*ECMAScript §15.9.3.2 new Date (value)*](http://ecma-international.org/ecma-262/5.1/#sec-15.9.3.2) – RobG Aug 05 '14 at 12:24
  • If I use subs-tractor "-" also, it works, will it return -1 now – Chakravarthy S M Aug 05 '14 at 12:37
  • Yes, it does, provided Date.parse gets it right (and likely it won't in all implementations in use). When using the `-` operator, there is no need for *getTime*. – RobG Aug 05 '14 at 12:40
  • Yes, may be, anyway thanks for the Info, I upvoted your answer – Chakravarthy S M Aug 05 '14 at 12:44