Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • sort() method of JavaScript Array

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 125
    Comment on it

    JavaScript Array sort() method : The sort() method is used to sort the elements of the array. By default sort() method sorts the elements in alphabetically ascending order. But in case of array contains the numeric values as a string then it will give the wrong result because in string 24 is greater than 100. To solve this problem you can create your own function to sort elements and pass it as a parameter. It changes the order of original array.


    Syntax of Array sort() method :

    array.sort(compareMethod)
    

    compareMethod : Optional. This function is used to define the alternative way to sort the elements. This method should return positive, negative or zero value.


    Example of Array sort() method : In this example how we can sort array elements in JavaScript using sort method.

    Sample1.html

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>To sort the array elements, click the button "sort".</p>
    <button onclick="sortElements()">sort</button>
    <p id="container"></p>
    
    <script>
    function sortElements() {
        var students = ["Ashok", "Nitin Kumar", "Rajesh", "Pankaj", "Sumit", "Manish", "Pramod"];
        students.sort();
        document.getElementById("container").innerHTML = students;
    }
    </script>
    
    </body>
    </html>
    

    Output :

    Ashok, Manish, Nitin Kumar, Pankaj, Pramod,Rajesh, Sumit
    

    In Sample2.html I will show you how to sort array elements in ascending order.


    Sample2.html

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>To sort the array elements in ascending order, click the button "sort".</p>
    <button onclick="sortElements()">sort</button>
    <p id="container"></p>
    
    <script>
    var marks = [20, 25, 21, 30, 26];
    document.getElementById("container").innerHTML = marks;
    
    function sortElements() {
        marks.sort(function(a, b){return a-b});
        document.getElementById("container").innerHTML = marks;
    }
    </script>
    
    </body>
    </html>
    

    Output :

    To sort the array elements in ascending order, click the button "sort".
    20,21,25,26,30
    

    In Sample3.html I will show you how to sort array elements in descending order.


    Sample3.html

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>To sort the array elements in descending order, click the button "sort".</p>
    <button onclick="sortElements()">sort</button>
    <p id="container"></p>
    
    <script>
    var marks = [20, 25, 21, 30, 26];
    document.getElementById("container").innerHTML = marks;
    
    function sortElements() {
        marks.sort(function(a, b){return b-a});
        document.getElementById("container").innerHTML = marks;
    }
    </script>
    
    </body>
    </html>
    

    Output :

    To sort the array elements in descending order, click the button "sort".
    30,26,25,21,20
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: