Skip to content
Related Articles

Related Articles

JavaScript Array sort() Method

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 01 Jun, 2022
View Discussion
Improve Article
Save Article

Below is the example of Array sort() method.

  • Program 1: 

JavaScript




<script>
// JavaScript to illustrate sort() function
function func() {
 
    // Original string
    var arr = ["Geeks", "for", "Geeks"]
 
    document.write(arr);
    document.write("<br>");
    // Sorting the array
    document.write(arr.sort());
}
func();
</script>


  • Output:
Geeks,for,Geeks
Geeks,Geeks,for

The arr.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: This method accept a single parameter as mentioned above and described below:

  • compareFunction: This parameters is used to sort the elements according to different attributes and in the 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. Below examples illustrate the JavaScript Array sort() method:

  • Example 1: In this example the sort() method arranges the elements of the array in ascending order.
var arr = [2, 5, 8, 1, 4]
document.write(arr.sort());
document.write(arr);
  • Output:
1,2,4,5,8
1,2,4,5,8
  • Example 2: In this example the sort() method the elements of the array are sorted according the function applied on each element.
var arr = [2, 5, 8, 1, 4]
document.write(arr.sort(function(a, b) {
  return a + 2 * b;
}));
document.write(arr);
  • Output:
2,5,8,1,4
2,5,8,1,4
  • Example 3: In this example, we use the sort() method on the array of numbers & observe some unexpected behaviour .
let numbers = [20,5.2,-120,100,30,0]
console.log(numbers.sort())
  • Output:
-120,0,100,20,30,5.2

Our output should be -120, 0, 5.2, 20, 30, 100 but it’s not so, why? Because as we apply directly sort() method, it would  processes accordingly: 100 would be placed before 20, as ‘2’ is larger than ‘1’ and similarly in case of 30 & 5.2,  as ‘5’ is larger than ‘3’ thus, 30 would be placed before 5.2. We can resolve this unexpected error by using sort() method for numerics using following compare function: 

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

Code for the above method is provided below: 

Program 1: 

JavaScript




<script>
// JavaScript to illustrate sort() function
function func() {
 
    //Original string
    var arr = [2, 5, 8, 1, 4]
 
    //Sorting the array
    document.write(arr.sort());
    document.write("<br>");
    document.write(arr);
}
func();
</script>


Output:

1,2,4,5,8
1,2,4,5,8

Program 2: 

JavaScript




<script>
// JavaScript to illustrate sort() function
 
function func() {
 
    // Original array
    var arr = [2, 5, 8, 1, 4];
 
    document.write(arr.sort(function(a, b) {
    return a + 2 * b;
}));
document.write("<br>");
document.write(arr);
}
func();
</script>


Output:

4,1,8,5,2
4,1,8,5,2

Supported Browsers: The browsers supported by JavaScript Array sort() method are listed below:

  • 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 sort() method varies & depends on implementation.
For example, in 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).


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!