Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array with object sorting with Underscore sortBy

I have this array. How do I use underscore '_.sortBy' to sort it according to start date?

[     {          id: 'oljw832021kjnb389xzll323jk',         start: { dateTime: '2013-09-26T13:30:00-07:00' },         end: { dateTime: '2013-09-26T14:30:00-07:00' },     },     {          id: 'ed7l5tmckdp0lm90nvr4is3d4c',         start: { dateTime: '2013-09-26T15:30:00-07:00' },         end: { dateTime: '2013-09-26T16:30:00-07:00' },     },     {          id: 'etmasdsackdp0kjl0nvrkopioqw',         start: { dateTime: '2013-09-26T18:00:00-07:00' },         end: { dateTime: '2013-09-26T19:00:00-07:00' },     } ] 
like image 869
Jack Dre Avatar asked Sep 30 '13 09:09

Jack Dre


People also ask

How do you use sortBy in JavaScript?

sortBy() function is used to sort all the elements of the list in ascending order according to the function given to it as a parameter. Passing the array with a function which returns the number and it will sort the array in ascending order and return an array. The array can be both of numeric values and string values.

How do I sort an array in OBJ?

To sort an array of objects, you use the sort() method and provide a comparison function that determines the order of objects.


1 Answers

Use an iterator function, not a single string for a property:

_.sortBy(arr, function(o) { return o.start.dateTime; }) 
like image 179
Bergi Avatar answered Sep 18 '22 14:09

Bergi