Lodash – orderBy method

Lodash - orderBy method

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 }
]

Next Topic – Click Here

Leave a Reply