js 睡眠排序

话说就要去面试了,又话说面试必考排序算法,所以。。。代码如下:

var arr=[4,2,7,5,6,0,8];
var sleepSort=function(arr,callback){
    let res=[];
    arr.forEach(item=>{
        setTimeout(()=>{    
            res.push(item) 
            // 如果执行完毕,回调
            if(res.length===arr.length) callback(res);
        },item)
    });
}
sleepSort(arr,res=>{console.log(res)})

猜你喜欢

转载自www.cnblogs.com/xingguozhiming/p/9775847.html