默认排序方法
默认排序是根据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'));