Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort Date in descending order in underscore.js? [duplicate]

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

like image 977
user3400887 Avatar asked Oct 20 '25 09:10

user3400887


2 Answers

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

like image 174
RoboBear Avatar answered Oct 22 '25 04:10

RoboBear


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

like image 33
Praveen Prasannan Avatar answered Oct 22 '25 04:10

Praveen Prasannan