array.sort() array.sort(orderfunc)
An optional function used to specify the sorting order.
Nothing.
The sort() method sorts the elements of array in place--i.e., no copy of the array is made. If sort() is called with no arguments, then the elements of the array are arranged in alphabetical order (more precisely: the order determined by the character encoding). To do this, elements are first converted to strings, if necessary, so that they can be compared.
If you want to sort the array elements in some other order, you must supply a comparison function that compares two values and returns a number indicating their relative order. The comparison function should take two arguments, a and b, and should:
The example section shows how you might write a comparison function to sort an array of numbers in numerical, rather than alphabetical order.
The following code shows how you can write an ordering function and use it to sort an array:
// An ordering function for a numerical sort function numberorder(a, b) { return a - b; } a = new Array(33, 4, 1111, 222); a.sort(); // Alphabetical sort: 1111, 222, 33, 4 a.sort(numberorder); // Numerical sort: 4, 33, 222, 1111
This HTML Help has been published using the chm2web software. |