Categories
JavaScript Answers

How to do multi-column sortBy with lodash and JavaScript?

Spread the love

Sometimes, we want to do multi-column sortBy with lodash and JavaScript.

In this article, we’ll look at how to do multi-column sortBy with lodash and JavaScript.

How to do multi-column sortBy with lodash and JavaScript?

To do multi-column sortBy with lodash and JavaScript, we can use the orderBy function.

For instance, we write

const data = _.orderBy(arr, ["type", "name"], ["asc", "desc"]);

to call orderBy with the arr array that we want to sort.

And we call it with 2 arrays that has the property we want to sort by and the direction.

Then the array with the sorted items is returned.

Conclusion

To do multi-column sortBy with lodash and JavaScript, we can use the orderBy function.

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 *