How would I sort an array of dates in chronological order? For example I have:
var dates = [
\'03/03/2014\',
Try this (feel free to ask for details) :
dates.sort(function (a, b) {
// '01/03/2014'.split('/')
// gives ["01", "03", "2014"]
a = a.split('/');
b = b.split('/');
return a[2] - b[2] || a[1] - b[1] || a[0] - b[0];
});
Translation of the last line :
return return
a[2] - b[2] years comparison if year A - year B is not 0
|| or
a[1] - b[1] months comparison if month A - month B is not 0
|| or
a[0] - b[0]; days comparison
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort.