Igor Simic
1 year ago

Sort array of object by object element value - JavaScript

Sometimes you want to sort array of objects by object string value - alphabetically. How to do it? 
Here is how!

For example, let's say we have a list of users and we want to sort the complete array by users.name, we will use function sort() and localeCompare():
users.sort((a, b) => a.name.localeCompare(b.name))

and that is it. 
Our array will be sorted alphabetically by users name value!

How to use this same method in TypeScript? Check this link: