Languages

JavaScript - sort array of arrays by string in it

0 points
Asked by:
Jun-L
963

I have an array that contains arrays and I would like to sort the arrays in order based on a certain string within those arrays.

For example:

I want it to be sorted alphabetically by name in the following order: 

1 answer
0 points
Answered by:
Admin Dirask Community
4380

You can pass a custom compare function to the sort() method.

Practical examples

ES6+ solution:

For older versions:

 

See also

  1. JavaScript - sort array

  2. JavaScript - sort array of strings

References

  1. Array.prototype.sort() - JavaScript | MDN
0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join