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