Sort () is a method of sorting an array, as an example:
var aa = [11,4,6,2,9,8,5];d ocument.write (Aa.sort ());
Results The return value is: 11,2,4,5,6,8,9, that is, less than 10 of the normal row, but more than 10 of the problem, after consulting the W3school said:
If the method is called without parameters, the elements in the array are sorted alphabetically, more precisely, by the order in which the characters are encoded. To do this, you should first convert the elements of the array to a string, if necessary, for comparison. If you want to sort by other criteria, you need to provide a comparison function that compares two values and returns a number that describes the relative order of the two values. The comparison function should have two parameters A and B with the return value as follows: 1. If a is less than B, a value less than 0 is returned if a should appear before B in the sorted array. 2. If a equals B, 0 is returned. 3. If a is greater than B, a value greater than 0 is returned.
In other words, when comparing numbers, they are converted to corresponding characters by default and then compared, so the problem arises, if you want to compare numbers, you need to provide a comparison function:
function Sortnumber (A, B) {//numeric comparison function//return a-b//}var AA = [11,4,6,2,9,8,5];aa.sort ( function (A, b) { return a -b//positive order, reverse b-a });d ocument.write (AA);
The result has a return value of: 2,4,5,6,8,9,11
The use of Js-sort sort