Sort an integer array in JavaScript
This post will discuss how to sort an array of integers in JavaScript.
There are several ways to sort an array of integers in JavaScript. Here are some examples of how to sort an array of integers in JavaScript given an array:
1. Using a compare function
This is a common technique that works in any version of JavaScript. It involves passing a custom compare function to the Array.sort() function, which defines how to compare two elements in the array. The compare function should return a negative, zero, or positive value, depending on whether the first element is less than, equal to, or greater than the second element. For example, if we have an array and we want to sort it in ascending order, we can do:
|
1 2 3 4 5 6 7 8 |
var numbers = [12, 5, 8, 4, 1]; numbers.sort(function(a, b) { // compare two elements by subtracting them return a - b; }); console.log(numbers); // [1, 4, 5, 8, 12] |
To sort an array in reverse order, we can return the opposite of what we would return for ascending order. Here’s an example:
|
1 2 3 4 5 6 7 8 |
var numbers = [6, 4, 1, 5, 8]; numbers.sort(function(a, b) { // compare two elements by subtracting them in reverse order return b - a; }); console.log(numbers); // [8, 6, 5, 4, 1] |
2. Using ES6 arrow function
The arrow function is a newer feature of JavaScript that allows writing concise functions with a shorter syntax. It involves using an arrow (=>) to define a compare function for the Array.sort() function. We can use the same logic as the previous example, but with a simpler syntax. Here’s an example:
|
1 2 3 4 5 6 |
var numbers = [12, 5, 8, 4, 1]; // compare two elements by subtracting them numbers.sort((a, b) => a - b); console.log(numbers); // [1, 4, 5, 8, 12] |
To sort an array in reverse order, we can compare two elements by subtracting them in reverse order. For example,
|
1 2 3 4 5 6 |
var numbers = [6, 4, 1, 5, 8]; // compare two elements by subtracting them in reverse order numbers.sort((a, b) => b - a); console.log(numbers); // [8, 6, 5, 4, 1] |
3. Using Intl.Collator object
This is another newer feature of JavaScript that allows sorting arrays according to the language and locale settings. It involves creating an instance of the Intl.Collator object with the desired options and using its compare function as the compare function for the Array.sort() function. For example, if we have an array and we want to sort it in ascending order according to the German numeric format (which uses a comma as the decimal separator), we can do:
|
1 2 3 4 5 6 7 8 9 |
var numbers = [12, 5, 8, 4, 1]; // create a collator object for German locale with numeric option var collator = new Intl.Collator('de-DE', {numeric: true}); // use the collator's compare function as the compare function numbers.sort(collator.compare); console.log(numbers); // [1, 4, 5, 8, 12] |
That’s all about sorting an array of integers in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)