数组之sort排序原理实现
2020-03-16 本文已影响0人
小雪洁
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>sort排序算法原理实现</title>
</head>
<body>
</body>
<script>
let d=[1,2,3];
for(const n in d){
for(const m in d){
//console.log(m);
}
}
let a=[3,8,2,5,9,0,4,7,1,34];
function sort(array,callback){
for(const n in array){
for(const m in array){
if(callback(array[n],array[m])<0){
let temp=array[m];
array[m]=array[n];
array[n]=temp;
}
}
//console.log("a"+n + ":" +array);
}
return array;
}
sort(a,function(a,b){
return a-b;
});
console.log(a);//[0, 1, 2, 3, 4, 5, 7, 8, 9, 34]
sort(a,function(a,b){
return b-a;
})
console.log(a);//[34, 9, 8, 7, 5, 4, 3, 2, 1, 0]
</script>
</html>