To sort an array in numerical order, use the function array.sort().
var myarray=[25, 101, 10, 33]
myarray.sort() //Array now becomes [10, 101, 25, 33]
The above code will see only the first number. So it returns the result as 10, 101, 25, 33
To Sort an array in numerical order or in ascending order
var myarray=[25, 101, 10, 33]
myarray.sort(function(a,b){return a - b})
Output:
Array becomes [10, 25, 33, 101]
To sort an array in numerical order, simply pass a custom sort function into array.sort() that returns the difference between "a" and "b", the two parameters indirectly/ automatically fed into the function.
|