Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptDifference between sorting string array and numerical array

Difference between sorting string array and numerical array

The array.sort() method sorts the string array in alphabetical order. This method sorts string arrays based on Unicode(). Unicode provides a unique number for every character, no matter what platform, device, application, or language. This allows every modern system to use Unicode. These should be started with uppercase and lowercase letters.

Syntax:

Array.sort()

Example 1: In this program, we are sorting the string array based on Unicode. We are taking a string array namely “strarray” which is sorted using JavaScript array sort() method. The elements of the array is joined into a string using join() method and shown in the “p” element.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" 
        content="IE=edge">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <script src=
    </script>
    <title>Sorting String array</title>
    <style>
        p#Input-Array{
           font-weight: bold;
        }
        h2{
            color:green ;
        }
        p#output{
            font-weight:bold ;
        }
    </style>
</head>
<body>
    <h2>neveropen</h2>
    <p>String array before Sorting</p>
    <p id="Input-Array">
        ['JavaScript','jQuery','WorkHome','Airport','GeeksForGeeks']
    </p>
    <p>String array after Sorting</p>
    <p id="output"></p>
  
    <script>
        $(document).ready(function(){
            var strarray = 
                ['JavaScript','jQuery','WorkHome','Airport','GeeksForGeeks'];
  
            strarray = strarray.sort();
            $('#output').html(strarray.join(' '));
        });
    </script>
</body>
</html>


Output:

 

If you use this method for sorting a numerical array, it does not work as you want because the sort method sorts an array using a Unicode basis. 

Numerical Array Sorting Example

We can see the sorting method does not work properly here because the sorting method uses sorting an array using a Unicode basis.

Example 2: The same approach of implementation is used in this example as in the first example.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content=
 "width=device-width, initial-scale=1.0">
    <script src=
      </script>
    <title>Sorting String array</title>
    <style>
        p#Input-Array{
           font-weight: bold;
        }
        h2{
            color:green ;
        }
        p#output{
            font-weight:bold ;
        }
    </style>
</head>
<body>
    <h2>neveropen</h2>
    <p>Numerical array before Sorting</p>
    <p id="Input-Array">['80','90','20','50','70',3]</p>
    <p>Numerical array after Sorting</p>
    <p id="output"></p>
  
    <script>
        $(document).ready(function(){
            var strarray = [80,90,20,50,70,3];
  
            strarray = strarray.sort();
           $('#output').html(strarray.join());
        });
    </script>
</body>
</html>


Output:

 

To sort numerical values correctly, we must define a comparison function with a sort.  If we define the comparison function then a pair of values from the array is repeatedly sent to compare function until all values are processed. A compare function should return a negative, zero, or positive depending upon the argument.

Syntax:

Array.sort(compare function)

Example 3: The following example uses the above approach with a user function as a parameter to the sort() function.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sorting String array</title>
    <style>
        p#Input-Array{
           font-weight: bold;
        }
        h2{
            color:green ;
        }
        p#output{
            font-weight:bold ;
        }
    </style>
</head>
<body>
    <h2>neveropen</h2>
    <p>Numerical array before Sorting</p>
    <p id="Input-Array">['80','90','20','50','70',3]</p>
    <p>Numerical array after Sorting</p>
    <p id="output"></p>
  
    <script>
        $(document).ready(function(){
            var strarray = [80,90,20,50,70,3];
  
            strarray = strarray.sort(function(a,b){
                return a-b;
            });
            $('#output').html(strarray.join());
        });
    </script>
</body>
</html>


Output:

 

Difference Between Numerical Sorting and String Sorting:

Numerical Sorting

String Sorting

If you want to sort a numerical array. You must use compare function inside in a sort method. In string sorting, you don’t need a compare function to a string array.
Numerical Array Sorting is not based on Unicode Basis  String Array Sorting is based on Unicode Basis
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments