Difference between sorting string array and numerical array
Last Updated :
22 Dec, 2022
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=
"https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js">
</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>GeeksforGeeks</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=
"https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js">
</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>GeeksforGeeks</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">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></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>GeeksforGeeks</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 |
Similar Reads
Difference between Heaps and Sorted Array 1. Heap:A heap is a tree based data structure in which tree should be almost complete. It is of two types i.e. max and min heap. Max heap: In max heap, if p is the parent and c is its child, then for every parent p the value of it is greater than or equal to the value of cMin heap In min heap, if p
4 min read
Difference Between ksort() and krsort() Functions Sorting an associative array by key is a common task in PHP, especially when you need to organize data alphabetically or numerically based on keys. PHP provides built-in functions to accomplish this task efficiently, allowing developers to rearrange the order of elements within an associative array
2 min read
How to Sort an Arrays of Object using Arrays.sort() To sort the array of objects in both ascending and descending order in Java, we can use the Arrays.sort() method with the Comparator. In this article, we will learn how to sort an array of objects using Arrays.sort(). Sorting an Array of ObjectWith Array of Object what it really means is an array st
3 min read
How to sort an Array in C# | Array.Sort() Method Set â 2 Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort(Array, Int32, Int32, IComparer) Method Sort(Array, Array, Int32, Int32, IComparer) Method Sort(Array, Int32, Int32) Method
9 min read
PHP Sort array of strings in natural and standard orders You are given an array of strings. You have to sort the given array in standard way (case of alphabets matters) as well as natural way (alphabet case does not matter).Input : arr[] = {"Geeks", "for", "geeks"}Output : Standard sorting: Geeks for geeks Natural sorting: for Geeks geeks Input : arr[] =
2 min read
How to Sort an Array in Scala? Sorting arrays effectively is essential for many applications, regardless of whether you're working with texts, custom objects, or numerical data. Because Scala is a strong and expressive language, it provides a variety of array sorting methods that may be customized to fit various needs and situati
5 min read
JavaScript - Sort an Array of Strings Here are the various methods to sort an array of strings in JavaScript1. Using Array.sort() MethodThe sort() method is the most widely used method in JavaScript to sort arrays. By default, it sorts the strings in lexicographical (dictionary) order based on Unicode values.JavaScriptlet a = ['Banana',
3 min read
How to Sort a Numerical String in TypeScript ? To sort numerical string in TypeScript, we could use localCompare method or convert numerical string to number. Below are the approaches used to sort numerical string in TypeScript: Table of Content Using localeCompareConverting to Numbers before sortingApproach 1: Using localeCompareThe localeCompa
1 min read
JavaScript - Sort a Numeric Array Here are some common methods to Sort Numeric Array using JavaScript. Please note that the sort function by default considers number arrays same as an array of stings and sort by considering digits as characters.Using sort() with a Comparison Function - Most UsedThe sort() method in JavaScript, when
2 min read
Sort Mixed Alpha/Numeric array in JavaScript Sorting a mixed alpha/numeric array in JavaScript involves arranging the elements in a specified order, typically lexicographically (dictionary order) for strings and numerically for numbers. When these types are mixed, a custom sort function is required to effectively handle comparisons between dif
3 min read