User Defined Sort Methods

Updated 2005-5-25

A user may define a function to sort an Array , the function is passed to the Array.sort() method as a parameter (without parentheses).

For example, in an Array containing numbers only, we may define the sorting algorithm ascending().

var nums=[2,4,5,22,100,56,220];// the array
function ascending(a,b){
return a-b;
}
nums.sort(ascending);

 

function descending(a,b){
return b-a;
}
nums.sort(descending);

More