The
Array's sort () method is used for sorting, and in this article you will introduce the specific use of the sort () method in JavaScript
The code is as follows: <html> <head> <title> Array Sort () method </title> <script> /* sort () 1, no duplicates, direct reference to the original array 2, if the method is called without using parameters, the elements in the alphabetical array are sorted, more precisely, sorted in the order of character encoding. To do this, you should first convert the elements of the array into strings (if necessary) for comparison. 3, if you want to sort by other criteria, you need to provide a comparison function that compares two values, and then returns a number that describes the relative order of the two values. The comparison function should have two parameters A and B, and its return value is: if A is less than B, a value less than 0 is returned if a is in the sorted array before B. If a equals B, it returns 0. If a is greater than B, a value greater than 0 is returned. */ var arr = [2,4,8,1,22,3]; var arrsort= arr.sort ();//not sorted correctly, array first converted to string and then sorted Docu Ment.write ("The default sort array is:" + arrsort);//1,2,22,3,4,8 document.write ("<br/>"); //comparison function function Mysort (a,b) { return a-b; } var arrSort2 = Arr.sort (mysort);//Incoming comparison function document.write ("The array of incoming comparison parameters is:" + arrSort2);/Right Sort document.write ("<br/>"); document.write (" The original array is: "+ arr); </script> ≪/head> <body> <div id= "Time" ></div> </body> </ html>