Syntax Of Lodash orderBy method
_.orderBy(collection, [iteratees=[_.identity]], [orders])
This Lodash orderBy method is like _.sortBy except that it allows specifying the sort orders of the iterates to sort by. If orders are 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.
Arguments
- collection (Array|Object) − The collection to iterate over.
- [iteratees=[_.identity]] (Array[]|Function[]|Object[]|string[]) − The iteratees to sort by.
- [orders] (string[]) − The sort orders of iteratees.
Output
- (Array) − Returns the new sorted array.
Example
var _ = require('lodash'); var users = [ { 'user': 'Joe', 'age': 48 }, { 'user': 'Robert', 'age': 34 }, { 'user': 'Julie', 'age': 40 }, { 'user': 'Stafey', 'age': 36 } ]; var result = _.orderBy(users, ['user', 'age'], ['asc', 'desc']); console.log(result);
Save the above program in tester.js. Run the following command to execute this program.
Command
\>node tester.js
Output
[ { user: 'Joe', age: 48 }, { user: 'Julie', age: 40 }, { user: 'Robert', age: 34 }, { user: 'Stafey', age: 36 } ]