js :数组排序 sort

2018-10-21  本文已影响0人  gis杭州

1 默认排序

var arr = ["a","b","c","z","y","x","bob","tom"]
var arrsort = arr.sort();
console.log(arrsort);//["a", "b", "bob", "c", "tom", "x", "y", "z"]
image.png

2 指定排序规则

2.1 对数字排序未指定规则时:

var numarr =[10,5,40,25,1000,1];
var numarrsort = numarr.sort();
console.log(numarrsort)
image.png

2.2 指定规则时:

function sortNumberFun(a,b)
{
return a - b
}
var numarr =[10,5,40,25,1000,1];
var numarrsort = numarr.sort(sortNumberFun);
console.log(numarrsort)
image.png

参考:http://www.w3school.com.cn/js/jsref_sort.asp

上一篇 下一篇

猜你喜欢

热点阅读