Look directly at the code, the test results are also posted inside
Copy code code as follows:
var arrdemo = new array ();
Arrdemo [0] = 10;
Arrdemo [1] = 50;
Arrdemo [2] = 51;
Arrdemo [3] = 100;
Arrdemo.sort (); // After calling the sort method, the array itself will be changed, that is, affect the original array
Alert (ARRDEMO); // 10,100,50,51 By default, the SORT method is sorted in the order of ASCII, not we think it is sorted in the size size of the number.
Arrdemo.sort (function (a, b) {Return a> B? 1: -1}); // Sort from small to large
Alert (Arrdemo); // 10,50,51,100
Arrdemo.sort (function (a, b) {return a <b? 1: -1}); // Sort from large to small
Alert (Arrdemo); // 100,51,50,10
in conclusion:
1. After calling the SORT method, it will affect itself (rather than generate a new array)
2. Sort () Method is sorted by character by default, so when sorting the digital array, you must not think of it for granted that it will be sorted according to the number size!
3. To change the default SORT behavior (that is, sort by character), you can specify the sorting rules function (as shown in this case)