var arr = [0,1,2,10,5,29,7] console.log(arr.sort(compare)) function compare(a,b) { if (a < b){ return -1 }else if(a == b){ return 0 }else { return 1 } }
或者修改compare函数
function compare(a,b) { return a - b; }
var arr = [0,1,2,10,5,29,7] console.log(arr.sort(compare)) function compare(a,b) { if (a < b){ return -1 }else if(a == b){ return 0 }else { return 1 } }
或者修改compare函数
function compare(a,b) { return a - b; }