How to Sorting in JavaScript

Posted By : Dipak Kumar Singh | 29-Oct-2018

Sorting mechanism in javascript refers to a model of simplicity. Sorting is also used for organizing the array not just into numerical or alphabetical order, but also in a logical and conditional arrangement. 

 

How the sort function works in javascript.

if you call the sort() with no argument, then your array will be sorted in a lexicographical order that means each value will be treated as a string see below example for explanation.

 

 
var letters = ["R","O","F","L"];
    
letters.sort();
    
alert(letters);    //produces ["F","L","O","R"]

so if you want to make comparison function then you have to return and perform a simple evaluation that will produce the desired sort let see the below example for that. 

 
var numbers = [8,5];
    
numbers.sort(function(a, b)
{
    return a - b; 
});
    
alert(numbers); 
 

let's deeply understand this example: first a=8 and b=5 then we did (a-b) = 3 that means three is greater than zero, hence b sorted before a and produce the order [5,8]


so we will inverse the numerical order.

 

 
var numbers = [4,3,5,9];
    
numbers.sort(function(a, b)
{
    return b - a; 
});
    
alert(numbers); 
 

if you want to create a comparison function which will produce a dictionary sort, each pair of a string will be evaluated in a computational term. if a is less than b so we can compare the strings directly like the below example. 

 
var letters = ["R","O","F","L"];
    
letters.sort(function(a, b)
{
    var x = a.toLowerCase(), y = b.toLowerCase();
    
    return x < y ? -1 : x > y ? 1 : 0;
});

 

see and understand here we have pre-convert each string to lower-case, which will ensure that we will get a case-insensitive sort, because if don't do this then the upper-case and lower-case letters will get sorted separately. Some browser objects can over-written the arguments, to overcome this we can assign the result of the operation to a new variable.


Multi-criteria sorting


Sorting mechanism in javascript refers to a model of simplicity. Sorting is also used for organizing the array not just into numerical or alphabetical order, but also in a logical and conditional arrangement .

How the sort function works in javascript.

if you call the sort() with no argument, then your array will be sorted in a lexicographical order that means each value will be treated as a string see below example for explanation.

 

 
var letters = ["R","O","F","L"];
    
letters.sort();
    
alert(letters);    //produces ["F","L","O","R"]
 

so if you want to make comparison function then you have to return and perform a simple evaluation that will produce the desired sort let see the below example for that.  

 
var numbers = [8,5];
    
numbers.sort(function(a, b)
{
    return a - b; 
});
    
alert(numbers);

let's deeply understand this example: first a=8 and b=5 then we did (a-b) = 3 that means three is greater than zero, hence b sorted before a and produce the order [5,8]


so we will inverse the numerical order

 

 
var numbers = [4,3,5,9];
    
numbers.sort(function(a, b)
{
    return b - a; 
});
    
alert(numbers); 
 

if you want to create a comparison function which will produce a dictionary sort, each pair of a string will be evaluated in a computational term. if a is less than b so we can compare the strings directly like the below example 

 
var letters = ["R","O","F","L"];
    
letters.sort(function(a, b)
{
    var x = a.toLowerCase(), y = b.toLowerCase();
    
    return x < y ? -1 : x > y ? 1 : 0;
});
 

see and understand here we have pre-convert each sting to lower-case, which will ensure that we will get a case-insensitive sort, because if don't do this then the upper-case and lower-case letters will get sorted separately. Some browser objects can over-written the arguments, to overcome this we can assign the result of an operation to a new variable


Multi-criteria sorting


Is there a possibility of sorting a multi-dimensional array with help of one value and can we sort them using both of their values. The answer to this question this yes we can do this just by adding the further conditions which are inside of comparison function. let take an example you can use value [0] for the primary sort, but in any case, you got two values equal then you can use [1] for the secondary sort. 


Let' see this shape example for better understanding 

 

 
var shapes = [
    [4, "Trapezium"],
    [5, "Pentagon"],
    [3, "Triangle"],
    [4, "Rectangle"],
    [4, "Square"]
    ];
    
shapes.sort(function(a, b)
{
    if(a[0] === b[0])
    {
        var x = a[1].toLowerCase(), y = b[1].toLowerCase();
        
        return x < y ? -1 : x > y ? 1 : 0;
    }
    return a[0] - b[0];
});
 

This kind of principal can be extended as far you can go. if the primary test result is equal then you can sort this by the second test and if secondary is also equal then you can sort by tertiary test and can go on and on for many kinds of comparisons

 

About Author

Author Image
Dipak Kumar Singh

Dipak is a skilled HTML Developer, expertise in UI Development. Dipak likes watching movies and playing computer games.

Request for Proposal

Name is required

Comment is required

Sending message..