sort()排序

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;
    }

猜你喜欢

转载自www.cnblogs.com/founderswitch/p/9490184.html