This article is mainly on the list of JS (array) of the sort (sort) Notes are described, the need for friends can come to the reference, I hope to help you
Look at the code directly, the test results are also posted inside the code as follows: var arrdemo = new Array (); Arrdemo[0] = 10; ARRDEMO[1] = 50; Arrdemo[2] = 51; ARRDEMO[3] = 100; Arrdemo.sort (); When the sort method is invoked, the array itself is changed, which affects the original array alert (Arrdemo),//10,100,50,51 By default the sort method is sorted in ASCII alphabetical order, rather than what we think is sorted by number size Arrdemo.sort ( function (a,b) {return a>b?1:-1});//Small to large sort alert (Arrdemo);//10,50,51,100 arrdemo.sort (function (a,b) {return a<b 1:-1});//from large to small sort alert (Arrdemo);//100,51,50,10 conclusion: 1. After the array calls the sort method, it affects itself (rather than generating a new array) the 2.sort () method is sorted by default, so the array of numeric Order, do not assume that will be sorted by number size! 3. To change the default sort behavior (that is, sort by character), you can specify the collation function yourself (as shown in this example)