lodash multi-column sortBy
As of lodash 3.5.0 you can use sortByOrder (renamed orderBy in v4.3.0):
var data = _.sortByOrder(array_of_objects, ['type','name'], [true, false]);
Since version 3.10.0 you can even use standard semantics for ordering (asc, desc):
var data = _.sortByOrder(array_of_objects, ['type','name'], ['asc', 'desc']);
In version 4 of lodash this method has been renamed orderBy:
var data = _.orderBy(array_of_objects, ['type','name'], ['asc', 'desc']);
You can always use Array.prototype.reverse()
var data = _.sortBy(array_of_objects, ['type', 'name']).reverse();
Deep field & multi field & different direction ordering Lodash >4
var sortedArray = _.orderBy(mixedArray,
['foo','foo.bar','bar.foo.bar'],
['desc','asc','desc']);
It's worth noting that if you want to sort particular properties descending, you don't want to simply append .reverse() at the end, as this will make all of the sorts descending.
To make particular sorts descending, chain your sorts from least significant to most significant, calling .reverse() after each sort that you want to be descending.
var data = _(data).chain()
.sort("date")
.reverse() // sort by date descending
.sort("name") // sort by name ascending
.result()
Since _'s sort is a stable sort, you can safely chain and reverse sorts because if two items have the same value for a property, their order is preserved.
More information
In the documentation of the version 4.11.x, says: ` "This method is like _.sortBy except that it allows specifying the sort orders of the iteratees to sort by. If orders is unspecified, all values are sorted in ascending order. Otherwise, specify an order of "desc" for descending or "asc" for ascending sort order of corresponding values." (source https://lodash.com/docs/4.17.10#orderBy)
let sorted = _.orderBy(this.items, ['fieldFoo', 'fieldBar'], ['asc', 'desc'])