4

Is there a function available in underscore.js to sort an array of date in descending order?

user3400887
  • 409
  • 1
  • 4
  • 18

2 Answers2

5

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

RoboBear
  • 5,434
  • 2
  • 33
  • 40
1

_.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.

Praveen Prasannan
  • 7,093
  • 10
  • 50
  • 70