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:
// ONLINE-RUNNER:browser;
const numbers = [10, 2, 5];
numbers.sort((a, b) => a - b);
console.log(numbers); // [2, 5, 10]
Sort in reverse order
To sort the array in reverse order, we need to change comparer.
// ONLINE-RUNNER:browser;
const numbers = [10, 2, 5];
numbers.sort((a, b) => b - a);
console.log(numbers); // [10, 5, 2]