数组排序相关
结合sort和函数排序:
数组由小到大进行排序:sort,sortnum;
vararr = [3,43,23,45,65,90];
function sortnum(a,b){
returna-b;
}
arr = arr.sort(sortnum);
console.log(arr);// [3, 23, 43, 45, 65, 90]
数组由大到小进行排序:sort,sortnum;
vararr = [3,43,23,45,65,90];
function sortnum(a,b){
returna+b;
}
arr = arr.sort(sortnum);
console.log(arr);// [90, 65, 45, 23, 43, 3]
网友评论