js :数组排序 sort

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"]
js :数组排序 sort_第1张图片
image.png

2 指定排序规则

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

var numarr =[10,5,40,25,1000,1];
var numarrsort = numarr.sort();
console.log(numarrsort)
js :数组排序 sort_第2张图片
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)
js :数组排序 sort_第3张图片
image.png

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

你可能感兴趣的:(js :数组排序 sort)