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