解构法
let abc = [3, 5, 8, 7, 6, 4,66,99,14,15,16];
for (let i = 0; i < abc.length - 1; i++) {
for (let j = 0; j < abc.length-i-1; j++) {
if (abc[j] < abc[j + 1]) {
[abc[j],abc[j+1]] = [abc[j+1],abc[j]] //解构赋值(前后调换位置)
}
}
}
console.log(abc);
打印出 [99, 66, 16, 15, 14,8, 7, 6, 5, 4,3]
网友评论