Javascript Sort Array of Integers

Very common requirement developers encounter while working with javascript arrays is to sort an array of integers. By default, an array containing digits will be considered as strings while sorting. This article demonstrates easy ways to sort an array of integers using different methods and various example illustrations.

Table of Contents:

Sort an Array of Integers by Creating a Typed Array

Read More About Typed Array

Example:-

Sort the array of integers [78,89,54,3,901,1177]

Code:-

let myArray = [78,89,54,3,901,1177];
//converting to a typed array of 64-bit floating point number
myArray = new Float64Array(myArray);
console.log("Before Sorting: " + myArray );
console.log("After Sorting: " + myArray.sort() );

Output:-

Before Sorting: 78,89,54,3,901,1177
After Sorting: 3,54,78,89,901,1177

Explanation:-

  • Here, in the above code, we use the sort() method after creating a typed array utilizing the constructor of Float64Array.

Sort an Array of Integers Implementing Compare with Sort

Javascript’s sort() method sorts all the array elements and returns the sorted array.

Example:-

Sort the array of integers [78,89,54,3,901,1177]

Code:-

let myArray = [78,89,54,3,901,1177];
console.log("Before Sorting: " + myArray );
// using sort with compare(), within compare() implement your way to sort
myArray.sort((firstElement, secondElement) => { return firstElement - secondElement;});
console.log("After Sorting: " + myArray );

Output:-

Before Sorting: 78,89,54,3,901,1177
After Sorting: 3,54,78,89,901,1177

Explanation:-

  • Here, we use the sort() with compare() method passed as an argument in the above code.
  • Within the compare() function, we implement a sorting algorithm to sort the particular array, which calls the sort().

Sort an array of integers using loops

In the below code, we will be implementing the Insertion Sort. Insertion sort is used to sort the elements such that values from the unsorted region are pulled and placed to the correctly sorted region of the data structure. The array is iterated starting from array[1]. The current element is compared with its predecessor. If the current element is smaller than its predecessor, all the greater elements are moved by one position to make space for the swapped element.

Example:-

Sort the array of integers [78,89,54,3,901,1177]

Code:-

var myArray = [78,89,54,3,901,1177];
for (var i = 0; i < myArray.length; i++) 
{// store the element to be compared in key 
    var key = myArray[i];
    for (var j = i - 1; j >= 0 && (myArray[j] > key); j--) 
    {
      // move the elements one place right to adjust the swapped element
        myArray[j+1] = myArray[j];
    }
    myArray[j+1] = key
}
console.log(myArray);

Output:-

Before Sorting: 78,89,54,3,901,1177
After Sorting: 3,54,78,89,901,1177

Explanation:-

  • Here insertion sort algorithm is used to compare and re-arrange elements in the array.

Read More:

I hope this article helped you sort an array of integers in javascript. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top