Sort (), an array object that provides a sorting method.
This method receives a function that receives two parameters and sorts them by the judgment of the two parameters.
if the A-B > 0, then the A and B. swap positions. Of course, the words are in ascending order. If you want a descending row to change the A-B to b-a can be achieved, that is, a is greater than a, B and a exchange position, the large value row in front. The value of a, B is the comparison from arr[0],arr[1], similar to the bubbling sort.
1 //Sort (), the array provides a sorting method. 2 //This method receives a function that receives two parameters and sorts them by the judgment of the two parameters. 3 vararr = [5, 50, 20, 30, 47];4Arr.sort (function(A, b) {5 returnAb;6 });7 Console.log (arr);8 /*if the A-B > 0, then the A and B. swap positions. Of course, the words are in ascending order. If you want a descending row, change the A-B to b-a.9 can be achieved, that is, B is greater than a, then B and a exchange position, the large value row front. The value of a, B is compared from arr[0],arr[1],Ten similar to bubble sort. */
Click here for the original
Js_sort () sorting an array element