MoreLesson 282Free preview
JS Sort Numeric Array
This lesson is part of How To · All levels
Summary
Method sort() in JavaScript changes arrays and by default sort them by string, not numerical value. The result [10, 1, 2] can be [1, 10, 2] because '10' is considered smaller than '2'. Give the comparison for the correct number order.
Key points
- Sort() without a comparator converts the element into a string and then compares the lexicographic sequence.
- I'm not going to.
- I'm not going to.
- Sort() modify the original array; use slice() or spread if you want to maintain it.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.