To use the orderBy
function from lodash, first we need to install it via npm:
index.tsx19 chars2 lines
Then, we can import it into our .js
file:
index.tsx29 chars2 lines
Now, we can use the orderBy
function to sort an array. Here's an example:
index.tsx339 chars15 lines
In the first example, we use ['name']
as the first argument to sort by the name
property. In the second example, we use ['age']
as the first argument and ['desc']
as the second argument to sort by the age
property in descending order.
Note that lodash's orderBy
can handle multiple properties and sort orders. For instance, we can sort by multiple keys:
index.tsx99 chars3 lines
Here, we sort by age
first in ascending order, and then by name
in descending order.
gistlibby LogSnag