JavaScript

How to Sort an Array of Integers Correctly

Arrays are objects that store multiple values ​​in a single variable. It can store multiple data types, including integers, strings, and so on. Developers sometimes need to organize data in order to make it easier to use. For arranging array elements, you can organize them in ascending or descending order.

This tutorial will describe the methods for correctly sorting an array of integers.

How to Sort an Array of Integers Correctly?

To correctly sort an array of integers, use the following methods:

Method 1: Sort an Array of Integers Correctly Using Compare Function in the sort() Method

For correctly sorting an array of integers, use the compare function in the “sort()” method. The compare function compares the integer values and sorts them at their positions. As we know, the sort() method sorts the elements in lexicographical or alphabetical order, which may not produce the desired results for an array of numbers.

Syntax

Follow the given syntax for sorting integer arrays with the help of compare function:

sort(function(x, y){return x - y}))

The above syntax:

• The returned value of the compare function is less than zero, “x” will be placed before “y”.
• If the value returned by the function is greater than zero means a positive number, the “y” will be placed before “x”.
• No element will change if (x – y == 0).

Let’s first see an example when the array is sorted using the only sort() method.

Example

Create an array of integers containing negative as well as positive integers:

var array = [-8, -4, -12, -0, 23, 4, 16, 8, 10, 14];

Call the sort() method:

console.log(array.sort());

It can be seen that the integers are sorted but not in any specific order (it is not in ascending order nor in descending order):

Now, use the compare function in sort() method to sort the elements in ascending order:

console.log(array.sort(function(x, y){return x - y}));

It can be observed that the array elements are sorted in ascending order:

If you want to sort array in descending order, change the compare function:

console.log(array.sort(function(x, y){return y - x}));

Output

Method 2: Sort an Array of Integers Correctly Using Bubble Sort

Another approach for sorting integer array is the sorting technique. Here, the “Bubble sort” will be discussed. For bubble sort, use the “while” loop that will iterate through the array until its length and sort every element based on the condition. The Bubble Sort examines two close elements/items and swaps them until the required order is obtained.

Example

Create variables “i”,”j,” and “temp” and initialize “i = 0”:

let i=0, j, temp;

For comparing elements and sorting them in their actual positions, use the two “while” loops. The first loop iterates the array until it reaches its length, while the second loop checks elements and swaps them based on conditions:

while (i < array.length) {

j = i + 1;
while (j < array.length) {
if (array[j] < array[i]) {
temp = array[I];
array[i] = array[j];
array[j] = temp;
}
j++;
}
i++;
}

Print the sorted array on the console:

console.log(array);

Output

For sorting an array in descending order using bubble sort, change the condition:

array[j] > array[i]

Or

array[i] < array[j]

That’s all about sorting the integer array in JavaScript.

Conclusion

For sorting the integer array correctly, use the “compare function” in the sort() method or the sorting technique called “Bubble sort”. Compare function in sort() method compares the integers in an array and sorts them at their positions. Similarly, Bubble Sort two examines two close elements/items and swaps them until the required order is obtained. This tutorial described the methods for correctly sorting an array of integers.