Array中有自带的排序功能,这个使用起来比较方便,我们有一点必须清楚,就是排序的依据,如果sort不传入参数的话,那就是按照字符编码(Unicode编码)的顺序排序。
var a=["3","2","1"];
console.log(a[0].charCodeAt(0)); // 51
console.log(a[1].charCodeAt(0)); // 50
console.log(a[2].charCodeAt(0)); // 49
console.log(a.sort()); // ["1", "2", "3"]
var a=["3","你","他"];
console.log(a[0].charCodeAt(0)); // 51
console.log(a[1].charCodeAt(0)); // 20320
console.log(a[2].charCodeAt(0)); // 20182
console.log(a.sort()); // ["3", "他", "你"]
var a=["3","11","222"];