Syntax, Parameter and Return Value
Syntax:
_.orderBy(collection, [iteratees=[_.identity]], [orders])
Parameters:
-
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.
Return Value:
(Array) - Returns the new sorted array.
Examples
Sorting by a Single Property:
JavaScript
var _ = require('lodash');
var users = [{ 'user': 'fred', 'age': 48 },
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 }];
var orderedByAge = _.orderBy(users, ['age'], ['asc']);
console.log(orderedByAge);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }, { 'user': 'fred', 'age': 48 }]
Demonstrates sorting an array of objects by the age property in ascending order.
Sorting by Multiple Criteria:
JavaScript
var orderedByNameThenAge = _.orderBy(users, ['user', 'age'], ['asc', 'desc']);
console.log(orderedByNameThenAge);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 48 }, { 'user': 'fred', 'age': 40 }]
Shows sorting first by user in ascending order, then by age in descending order.
Sorting Using a Custom Function:
JavaScript
var items = [1.1, 2.3, 1.4];
var orderedByFloorValue = _.orderBy(items, [Math.floor], ['asc']);
console.log(orderedByFloorValue);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[1.1, 1.4, 2.3]
An example of sorting numbers by their integer part in ascending order.
Complex Sorting of Nested Objects:
JavaScript
var products = [{ 'details': { 'name': 'apple', 'weight': 0.5 }},
{ 'details': { 'name': 'pear', 'weight': 0.3 }}];
var orderedByWeight = _.orderBy(products, ['details.weight'], ['desc']);
console.log(orderedByWeight);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'details': { 'name': 'apple', 'weight': 0.5 }}, { 'details': { 'name': 'pear', 'weight': 0.3 }}]
Demonstrates sorting an array of objects with nested properties.
Frequently Asked Questions
How does _.orderBy() handle null and undefined values?
_.orderBy() will sort null and undefined values based on JavaScript's < and > comparison rules, typically placing them at the end in ascending order.
Can _.orderBy() be used with different types of collections?
Yes, it can sort arrays and array-like objects, but the most common use case is sorting arrays of objects based on specific properties.
Is _.orderBy() efficient for large datasets?
While _.orderBy() is generally efficient, its performance may vary based on the complexity of the iteratees and the size of the dataset.
Conclusion
Lodash's _.orderBy() method is a flexible and powerful tool for sorting collections based on multiple criteria. It offers an intuitive and customizable approach to ordering elements, catering to complex sorting needs in JavaScript.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.