Thursday 6 June 2013

difference between sorting string array and sorting numerical array in jQuery

The sort method is used to sort any array elements. It sorts the string elements alphabetically.

For example

$(document).ready(function(){
var mylist = [ “Apple”,”Orange”,”Banana”];
mylist = mylist.sort();
$(“#mydiv”).html(list.join(“”));
});

It will give following output

Apple
Banana
Orange

Now we declare a numerical array and use sort() method to sort its elements.

$(document).ready(function(){
var mylist = [ “20”,”3””100”,”50”];
mylist = mylist.sort();
$(“#mydiv”).html(list.join(“”));
});

It will give following output

100
20
3
50

No comments:

Post a Comment