I am trying to sort an array that looks like this:
var dateGroups = [ [ {age:20, date: Fri Feb 03 2012 14:30:00 GMT+1100 (EST)}, {age:12, date: Fri Feb 03 2012 18:20:00 GMT+1100 (EST)}, {age:18, date: Fri Feb 03 2012 21:43:00 GMT+1100 (EST)} ], [ {age:32, date: Fri Feb 01 2012 10:54:00 GMT+1100 (EST)}, {age:44, date: Fri Feb 01 2012 11:45:00 GMT+1100 (EST)}, ], [ {age:22, date: Fri Feb 05 2012 10:54:00 GMT+1100 (EST)}, {age:22, date: Fri Feb 05 2012 18:22:00 GMT+1100 (EST)}, ] ]
The objects inside the nested dateGroups arrays are already sorted in ascending order, but I also want to sort the arrays themselves based on grouped dates.
In this case, the array should look like this:
var dateGroups = [ [ {age:32, date: Fri Feb 01 2012 10:54:00 GMT+1100 (EST)}, {age:44, date: Fri Feb 01 2012 11:45:00 GMT+1100 (EST)}, ], [ {age:20, date: Fri Feb 03 2012 14:30:00 GMT+1100 (EST)}, {age:12, date: Fri Feb 03 2012 18:20:00 GMT+1100 (EST)}, {age:18, date: Fri Feb 03 2012 21:43:00 GMT+1100 (EST)} ], [ {age:22, date: Fri Feb 05 2012 10:54:00 GMT+1100 (EST)}, {age:22, date: Fri Feb 05 2012 18:22:00 GMT+1100 (EST)}, ] ]
The function used for sorting should also return a new sorted version of dateGroups.
I tried using Underscore.js sortBy()
, but I cannot figure out how to sort arrays based on the value of a property inside one of the objects. Is there a way to sort Date objects? Or are they sorted like numbers or letters?