1
2
3
4
5
6
7
8
9
|
const arr = [1, 7, 0, 5, 6, 4, 2, 9, 3, 8];
// [1, 7, 0, 5, 6, 4, 2, 9, 3, 8]
console.log(arr.slice().sort((a, b) => 0)); // 保持不变
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(arr.slice().sort((a, b) => a - b)); // 升序
// [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
console.log(arr.concat().sort((a, b) => b - a)); // 降序
// [3, 6, 5, 2, 9, 7, 1, 0, 4, 8]
console.log(arr.concat().sort((a, b) => 0.5 - Math.random())); // 伪随机
|