![]() |
VOOZH | about |
Lodash _.orderBy() method is similar to the _.sortBy() method except that it allows the sort orders of the iterates to sort by. If orders are unspecified, then all values are sorted in ascending order otherwise order of corresponding values specifies an order of "desc" for descending or "asc" for ascending sort.
_.orderBy(Collection, [ iteratees ], [ orders ]);This method returns the new sorted array.
Example 1: In this example, we are sorting the array's patron in ascending order and age in descending order.
Output:
[
{ 'patron': 'john', 'age': 40 },
{ 'patron': 'john', 'age': 34 },
{ 'patron': 'jonny', 'age': 48 },
{ 'patron': 'jonny', 'age': 36 }
]
Example 2: In this example we uses _.orderBy() from lodash to sort the users array first by the employee field in ascending order, then by salary in descending order.
Output:
[
{ 'employee': 'hunny', 'salary': 60000 },
{ 'employee': 'hunny', 'salary': 55000 },
{ 'employee': 'munny', 'salary': 40000 },
{ 'employee': 'munny', 'salary': 36000 }
]