Copy code code as follows:
var value = [0,1,5,10,15];
Values.sort ();
Alert (Values); // Output 0,1,10,15,5
This is because Sort will call each Tostring method for comparison. "10" is smaller than "5", so in the front.
To sort the numerical value, a comparison function is required and the function is transmitted into the sort.
Copy code code as follows:
Function Compare (Value1, Value2) {
if (Value1 <Value2) {
Return -1;
} Else if (value1> value2) {{
Return 1;
} Else {
Return 0;
}
}
var value = [0,1,5,10,15];
Values.sort (compare);
Alert (Values); // Output 0,1,5,10,15
This is a positive, and it only needs to exchange the comparative functions-1 and 1 in the reverse.