The sort function is used to sort the elements of an array
Syntax: Arrayobject.sort (sortby);
The parameter sortby is optional and specifies the sort order, which must be a function.
If the method is called without parameters, it is sorted in the order of the subtitles, that is, in the order in which the letters are encoded. To do this, convert the elements of the array into strings 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 positions of these two numbers:
The comparison function should have two parameters A and B, and its return value: If A is less than B, a value that is less than 0 is returned if a is smaller than the number in the sorted digit before B, and A=b returns 0;A>B, which returns a value greater than 0.
function () {
<script type= "Text/javascript" > function Compare (A, a) {return a-B ; }var arr=[1,2,3,4,5,21,15];d ocument.write (arr+ ' <br/> ') document.write (Arr.sort (Compare));</script>
JavaScript function sort