I have this array. How do I use underscore \'_.sortBy\' to sort it according to start date?
[
{
id: \'oljw832021kjnb389xzll323jk\',
star
I did it this way:
var sorted = _(list).sortBy(
function (item) {
return [new Date(item.effectiveDate).getTime(), item.batchId];
}), "batchId");
If you want it descending then it's the same thing but *-1
var sorted = _(list).sortBy(
function (item) {
return [new Date(item.effectiveDate).getTime()*-1, item.batchId];
}), "batchId");
In this example I am ordering by two fields, you can forget about the item.batchId.
Hope this helps someone.
Use an iterator function, not a single string for a property:
_.sortBy(arr, function(o) { return o.start.dateTime; })
var sortedItem = _.sortBy(yourArrayName, ["start"])