Is there a function available in underscore.js to sort an array of date in descending order?
A more complete answer has been asked on Stack Overflow before, fwiw. I think this is also a great answer to the question, provided by @Bergi :
It's the same answer as @Praveen's, but it includes an example detailing the property checking for date comparisons, i.e. if you're "time model" is {"start": {"dateTime": ...}, "end": {"dateTime": ...} }, then you can sort via:
_.sortBy(arr, function(o) { return o.start.dateTime; })
Thanks to @Praveen and @Bergi!
StackOverflow QA Thread - Underscore, SortBy, Dates
_.sortBy
Splits a collection into sets, grouped by the result of running each value through iteratee. If iteratee is a string instead of a function, groups by the property named by iteratee on each of the values.
For sorting desc, you can reverse the array after sorting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With