Open In App
Related Articles

JavaScript Array sort() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

JavaScript Array.sort() Method is used to sort the array in place in a given order according to the compare() function. If the method is omitted then the array is sorted in ascending order.

Syntax:

arr.sort(compareFunction);

Parameters:

  • compareFunction: This parameter is used to sort the elements according to different attributes and in a different order.
    • compareFunction(a,b) < 0
    • compareFunction(a,b) > 0
    • compareFunction(a,b) = 0

Return value:

This method returns the reference of the sorted original array.

Example 1: This example shows the use of the sort() function.

JavaScript

// JavaScript to illustrate sort() function
function func() {
 
    // Original string
    let arr = ["Geeks", "for", "Geeks"]
 
    console.log(arr);
    // Sorting the array
    console.log(arr.sort());
}
func();

                    

Output
[ 'Geeks', 'for', 'Geeks' ]
[ 'Geeks', 'Geeks', 'for' ]

Example 2: In this example, the sort() method arranges the elements of the array in ascending order.

JavaScript

// JavaScript to illustrate sort() function
function func() {
    //Original string
    let arr = [2, 5, 8, 1, 4]
 
    //Sorting the array
    console.log(arr.sort());
    console.log(arr);
}
func();

                    

Output
[ 1, 2, 4, 5, 8 ]
[ 1, 2, 4, 5, 8 ]

Example 3: In this example, the sort() method the elements of the array are sorted according to the function applied to each element.

JavaScript

// JavaScript to illustrate sort() function
function func() {
 
    // Original array
    let arr = [2, 5, 8, 1, 4];
    console.log(arr.sort(function (a, b) {
        return a + 2 * b;
    }));
    console.log(arr);
}
func();

                    

Output
[ 2, 5, 8, 1, 4 ]
[ 2, 5, 8, 1, 4 ]

Example 4: In this example, we use the sort() method on the array of numbers & observe some unexpected behavior.

Javascript

let numbers = [20, 5.2, -120, 100, 30, 0]
console.log(numbers.sort())

                    

Output
[ -120, 0, 100, 20, 30, 5.2 ]

Explanation: Our output should be -120, 0, 5.2, 20, 30, 100 but it’s not so, why? Because as we apply the direct sort() method, it would process accordingly: 100 would be placed before 20, as ‘2’ is larger than ‘1’, and similarly in the case of 30 & 5.2,  as ‘5’ is larger than ‘3’ thus, 30 would be placed before 5.2.

Example 5: We can resolve the unexpected error that occured in the above example by using the sort() method for numerics using the following compare function.

Javascript

let numbers = [20, 5.2, -120, 100, 30, 0];
 
/* Logic:
   20 - (5.2) = +ve => 5.2 would be placed before 20,
   20 - (-120) = +ve => -120 would be placed before 20,
   20 - (100) = -ve => 100 would be placed after 20,
   20 - (30) = -ve => 30 would be placed after 20,
   20 - (0) = +ve => 0 would be placed before 20,
   Similarly for every element, we check and place them accordingly in iterations.
*/
 
function compare(a, b) {
    return a - b;
}
console.log(numbers.sort(compare));

                    

Output
[ -120, 0, 5.2, 20, 30, 100 ]

Please go through this How to Sort Numeric Array using JavaScript?, to know how the javascript array sort function works 

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers:

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 5.5 and above
  • Opera 4 and above
  • Safari 1 and above

Time Complexity: The time complexity of the sort() method varies & depends on implementation.

For example, in the Firefox web browser, it uses the merge sort implementation which gives time complexity as O(nlog n). Whereas, in Google Chrome web browser, it uses the Timsort implementation (a hybrid of merge sort and insertion sort), gives time complexity is O(nlogn).

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.



Last Updated : 28 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads