By default, the sort () method arranges an array item in ascending order-that is, the smallest value is at the front and the largest value is on the last side. To implement sorting, the sort () method invokes the ToString () transformation method for each array item, and then compares the resulting string to determine how to sort. Even if each item in the array is a numeric value, the sort () method compares the string as follows:
The code is as follows |
Copy Code |
var value=[8,10,0,5,20]; Value.sort (value); alert (value);//0,10,20,5,8 |
Visible, even though the order of the values in the example is not a problem, the sort () method changes the original order based on the result of the test string. Because the value 5 is less than 10, but before the string comparison is 5 bits in front of 10, the order of the array is modified, needless to say, this sort of arrangement is not the best scenario in many cases. So the sort () method can receive a comparison function as an argument so that we specify which value is in front of that value.
The comparison function receives two arguments and returns a negative number if the first argument should precede the second argument, or 0 if the two arguments are equal, and returns a positive number if the first argument is after the second argument. The following is a simple comparison function:
The code is as follows |
Copy Code |
<script type= "Text/javascript" >
function Compare (value1,value2) { if (value1<value2) { return-1; }else if (value1>value2) { return 1; }else{ return 0; } } function Gradedown (value1,value2) { if (value1<value2) { return 1; }else if (value1>value2) { return-1; }else{ return 0; } } var values=[0,8,4,9,15]; var down=[0,8,4,9,15]; Values.sort (Compare); Down.sort (Gradedown); document.write ("Ascending:" +values+ ' <br/> ') document.write ("Descending:" +down+ ' <br/> '); var value=[8,10,0,5,20]; Value.sort (); alert (value); </script> |
You can use a simpler comparison function for numeric types or for their valueof () methods to return object types for numeric types. This function subtracts the first value as long as the second value.
The code is as follows |
Copy Code |
Function va (value1,value2) { return value2-value1; }//Ascending function Vac (value1,value2) { return value1-value2; }//Descending var co=[88,22,44,11,20] Co.sort (VA) var ka=[99,22,44,88,23] Ka.sort (VAC) document.write (co+ ' <br> '); document.write (KA) |
Using the Reveres () method is faster if you just want to reverse the original order of the array.
As follows:
The code is as follows |
Copy Code |
var arra = [6,2,4,3,5,1]; /**//*arra.sort (); Document.writeln (ARRA); */ Function desc (x,y) ... { if (x > Y) return-1; if (x < y) return 1; } function ASC (X,Y) ... { if (x > Y) return 1; if (x < y) return-1; } Arra.sort (DESC); Sort by desc Document.writeln (ARRA); Document.writeln ("<br>"); Arra.sort (ASC); Sort by ASC Document.writeln (ARRA); Output results: 6,5,4,3,2,1 1,2,3,4,5,6 |
Alternatively, a nameless function can be directly placed in a call to the sort () method. The example below is an odd number in front and an even number in the following example:
The code is as follows |
Copy Code |
var arra = [6,2,4,3,5,1]; Arra.sort (function (x, y) ... { if (x 2 ==0) & nbsp; return 11; if (x% 2!=0) Return-1; } ); Document.writeln (ARRA); //output: 1,5,3,4,6,2 |