EN
JavaScript - sort array of integers
3 points
In this article, we would like to show you how to sort an array of integers in JavaScript.
Quick solution:
xxxxxxxxxx
1
const numbers = [10, 2, 5];
2
numbers.sort((a, b) => a - b);
3
4
console.log(numbers); // [2, 5, 10]
To sort the array
in reverse order, we need to change comparer.
xxxxxxxxxx
1
const numbers = [10, 2, 5];
2
numbers.sort((a, b) => b - a);
3
4
console.log(numbers); // [10, 5, 2]