共计 501 个字符,预计需要花费 2 分钟才能阅读完成。
默认排序方法
默认排序是根据 UniCode 码的顺序排序
var arr = ['afk','hello','body','script'];
arr.sort();
//['afk','body','hello','script']
升序排列
function sortNum(a,b) {rerturn a - b;}
var arr = [12,222,666,888,20,8];
arr.sort(sortNum);
//[8,12,20,222,666,888]
降序排列
function sortNum(a,b) {return b - a;}
var arr = [12,222,666,888,20,8];
arr.sort(sortNum);
//[888,666,222,20,12,8]
按照数组对象的某个属性值排序
var arr = {{name:leemo, age:21},
{name:chuchu, age:22},
{name:qiyue, age:1}
}
// 根据 age 排序
function compare(age){return function(a,b){return a[age] - b[age];
}
}
arr.sort(compare('age'));
正文完
发表至: javascript
2019-07-31