I have a backbone collection which has a bunch of models with date attributes associated with to them. I want to sort them based on their dates. So the latest dates first and so on.
Whats the best way to go around this.
The dates are formatted like this, a basic date object. Date {Mon Mar 05 2012 23:30:00 GMT-0500 (EST)}
Thanks
You have Date objects so you can use
getTimeto convert them to numbers and then negate those numbers to get the most-recent dates first. If you want to keep your collection sorted then a comparator like this:will do the trick. Demo (open your console please): http://jsfiddle.net/ambiguous/htcyh/
Backbone collections also include Underscore’s
sortByso you could do a one-time sort:Demo: http://jsfiddle.net/ambiguous/FF5FP/
Or you could use
toArrayto get a normal JavaScript array and use the standardsortwithout usinggetTime:Demo: http://jsfiddle.net/ambiguous/QRmJ4/