- Lodash Tutorial
- Lodash - Home
- Lodash - Overview
- Lodash - Environment Setup
- Lodash - Array
- Lodash - Collection
- Lodash - Date
- Lodash - Function
- Lodash - Lang
- Lodash - Math
- Lodash - Number
- Lodash - Object
- Lodash - Seq
- Lodash - String
- Lodash - Util
- Lodash - Properties
- Lodash - Methods
- Lodash Useful Resources
- Lodash - Quick Guide
- Lodash - Useful Resources
- Lodash - Discussion
Lodash - orderBy method
Syntax
_.orderBy(collection, [iteratees=[_.identity]], [orders])
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.
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 } ]
lodash_collection.htm
Advertisements