写一个简单的sort方法(只有冒泡没有快速排序)

let arr=[1,2,4,5,3];
    Array.prototype.NewSort=function(func){
        var flag;
        for(var i=0;i<this.length-1;i++){
            for(var j=1;j<this.length-i;j++){
                flag=func(this[j-1],this[j])>0?true:false;
                console.log(flag,this);
                if(flag){
                    [this[j-1],this[j]]=[this[j],this[j-1]];
                }
            }
        }
        return this;
    }
    let result=arr.NewSort(function(a,b){
        return a-b;
    })
    console.log(result);
posted @ 2022-03-24 14:40  听风小弟  阅读(49)  评论(0)    收藏  举报