写一个简单的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);

浙公网安备 33010602011771号