The sort function method in JavaScript is to return an Array object that has been sorted by an element. How to use:
arrayobj.sort(sortfunction)
Where Arrayobj is a required option. Any Array object.
Sortfunction is optional. is the name of the function used to determine the order of elements. If this argument is omitted, then the elements are sorted in ascending order in ASCII characters.
The sort function method in JavaScript sorts the array object appropriately, and does not create a new array object during execution.
If you provide a function for the sortfunction parameter, the function must return one of the following values:
Negative value if the first argument passed is smaller than the second argument.
0 if two parameters are equal.
Positive value if the first argument is larger than the second argument.
Example
The following example illustrates the use of the sort function method in javascript:
function SortDemo(){
var a, l; // 声明变量。
a = new Array("X" ,"y" ,"d", "Z", "v","m","r");
l = a.sort(); // 排序数组。
return(l); // 返回排序的数组。
}