Spread the love
To use JavaScript Lodash to sort array of object by value, we use the sortBy
method.
For instance, we write
const myArray = [ { id: 25, name: "Anakin Skywalker", createdAt: "2022-04-12T12:48:55.000Z", updatedAt: "2022-04-12T12:48:55.000Z", }, { id: 1, name: "Luke Skywalker", createdAt: "2022-04-12T11:25:03.000Z", updatedAt: "2022-04-12T11:25:03.000Z", },
]; const myOrderedArray = _.sortBy(myArray, (o) => o.name);
to call sortBy
with myArray
and a callback that returns the name
property value to sort by the name
property.
An array with the sorted values is returned.