1 <!DOCTYPE HTML>2 <HTML>3 <Body>4 <Script>5 varArray= NewArray ();6 for(varI= 0; I<Ten; I++)7 {8 Array[i]= NewArray ();9 array[i][0] =Math.floor (Math.random ()*Ten);Ten array[i][1] =Math.floor (Math.random ()*Ten); One } A document.write ("before sorting:"); - display (); - Array.Sort (functionCompare (V1,V2) the { - if(v1[0] >v2[0]) - { - return 1; + } - Else if(v1[0] <v2[0]) + { A return -1; at } - Else - { - if(v1[1]>v2[1]) - return 1; - Else if(v1[1]>v2[1]) in return -1; - Else to return 0; + } - }); the document.write ("after sorting:"); * display (); $ functiondisplay ()Panax Notoginseng { - for(varI= 0; I<Ten; I++) the { + document.write ("<br>"); A document.write (" "); the document.write (array[i][0]); + document.write (" "); - document.write (array[i][1]); $ } $ } - </Script> - </Body> the </HTML>View Code
The sort function in JS needs to pass in a function, such as sort (function (x, y) {}). X, Y is the two value of the array that needs to be sorted, and can be returned according to the size of X, Y:
A negative value, if the first argument passed is smaller than the second one.
0, if two parameters are equal.
Positive value if the first argument is larger than the second argument.
If positive, the X, y order is swapped, and 0 or-1 does not swap for x, Y.
JS Multiple sorting