How to Sort an Array of Numbers in JavaScript | How to Sort an Array of Strings in JavaScript

preview_player
Показать описание
How to Sort an Array of Numbers in JavaScript, How to Sort an Array of Strings in JavaScript.
Рекомендации по теме
Комментарии
Автор

By default, the sort method sorts elements alphabetically. So yours "solution" is not correct.
Try to sort this array: [1, 11, 2, 23, 5] in your way ...

For numeric values you have to use:
[1, 11, 2, 23, 5].sort(function(a, b) { return a-b;})