Skip to content
Related Articles

Related Articles

JavaScript Array sort() Method

Improve Article
Save Article
Like Article
  • Last Updated : 04 Oct, 2021

Below is the example of Array sort() method.

  • Program 1:




    <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
    • Then a comes before b in the answer.

    • compareFunction(a,b) > 0
    • Then b comes before a in the answer.

    • compareFunction(a,b) = 0
    • Then the order of a and b remains unchanged.

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
    
  • Code for the above method is provided below:
    Program 1:




    <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:




    <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

    My Personal Notes arrow_drop_up
    Recommended Articles
    Page :

    Start Your Coding Journey Now!