标签:
1、数组的sort()方法默认是按照字母排序的,下面举个栗子说明:
1)全是字母:
var arr =["zu","fan","an","peng","zhang"]; arr.sort(); alert(arr);//结果是an,fan,peng,zhang,zu
2)全是数字:
var arr =["34","12","67","44","668"]; arr.sort(); alert(arr);//结果是12,34,44,668,67
3)字母数字组合:
var arr =["34","a2","5","y7","6k"]; arr.sort(); alert(arr);//结果是34,5,6k,a2,y7(显示按照数字排序,然后在字母排)
4)sort()方法可以对数组进行升(降)序排序:
var arr =["34","12","67","44","668"];
arr.sort(function(a,b){
return a-b; (若换成b-a,则是降序排列)
});
alert(arr);//12,34,44,67,668
标签:
原文地址:http://www.cnblogs.com/imsomnus/p/4638214.html