9.20.2012

string array vs numerical array sorting

Difference between sorting string array and numerical array in jQuery
S.No
jQuery.sort() for string array
jQuery.sort() for numerical array
1
sort() method sorts the string array in alphabetical order.
sort() method does not sort the numerical array in proper order. This is because sort method sorts on the basis of the ASCII values .Hence,the numerical values are not sorted correctly with the sort() method i.e., it considers the ASCII value of the first numerical digit of all numerical values for sorting purposes.
2
To sort string array, there is no need to define a comparison function with sort().But, it is better to have all uniform names. That is, they must begin with either uppercase or lowercase, but not mixed case.
To sort numerical values correctly, we must define a comparison function with sort().

Note: Please look at the points below for comparison function process.

If we define a comparison function, then a pair of values from the array will be repeatedly sent to the function until all elements of the array are processed. In the comparison function, we thus write a statement considering the pair of values passed to it so that the function returns any of the following three values: <0 or="or">0.
  • When the function returns value is less than(<) 0, the second value of the pair of array values sent to the function is larger than the first value and hence must be pushed down the sorting order.
  • When the function returns value >0, the first value is larger than the second value, so it must be pushed down the sorting order.
  • When the function returns value =0, it means there is no need to change the sort order since the two values are same.
Example: jQuery.sort() for string array

$(document).ready(function() {
  var list= [ "jQuery", "Dojo", "JavaScript", "Ajax"];
  $('#alltech').html(list.join(""));
  list = list.sort();
  $('#sorted').html(list.join(""));
})
 
Results:

List of Client side technologies

jQuery
Dojo
JavaScript
Ajax

List of Client side technologies in sorted order

Ajax
Dojo
JavaScript
jQuery


Example: jQuery.sort() for numerical array without using comparison function

$(document).ready(function() {
  var list= [ 45, 32, 98, 24, 16, 9, 3];
  $('#allnumber').html(list.join(""));
  list = list.sort();
  $('#sorted').html(list.join(""));
})
 
Results:

Numbers in Array before sorting

45
32
98
24
16
9
3

Numbers in Array After sorting

16
24
3
32
45
9
98



Well, the numerical values are not sorted correctly with the sort() method because as mentioned earlier that it considers the ASCII value of the first numerical digit of all numerical values for sorting purposes. To sort numerical values correctly, we must define a comparison function with sort().

Example: jQuery.sort() for numerical array with comparison function

$(document).ready(function() {
  var list= [ 45, 32, 98, 24, 16, 9, 3];
  $('#allnumber').html(list.join(""));
  list = list.sort(function(a,b){
        return a-b;
    });
  $('#sorted').html(list.join(""));
})
 
Results:

Numbers in Array before sorting

45
32
98
24
16
9
3

Numbers in Array After sorting

3
9
16
24
32
45
98


Reference:


No comments:

Post a Comment