Categories
JavaScript Answers

How to do array with object sorting with JavaScript Underscore sortBy?

Spread the love

To do array with object sorting with JavaScript Underscore sortBy, we call sortBy with the array and a callback to return the field to sort by.

For instance, we write

const sorted = _.sortBy(arr, (o) => {
  return o.start.dateTime;
});

to call sortBy with arr and a callback that returns the start.dateTime property from the object to sort by this property.

An array with the sorted objects is returned.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *