一 、借用原型链补充数组的高阶排序方法
代码截图如下:
打印结果如下:
js源码如下:
// 借用原型链补充数组的高阶排序方法
Array.prototype.selectSort = function(){
const len = this.length
for (let i = 0; i < len - 1; i++) {
let minIndex = i
let minValue = this[i]
for (let j = i + 1; j < len; j++) {0
if (this[j] < minValue) {
minIndex = j
minValue = this[j]
}
}
this[minIndex] = this[i]
this[i] = minValue
}
return this
}
console.log([5,1,62,31,26,59,-12,0,12].selectSort())
// 排序后 [-12, 0, 1, 5, 12, 26, 31, 59, 62]
二 、扁平化数组 , 去重后,再排序
代码截图如下:
打印结果如下:
js源码如下:
// 扁平化数组 , 去重后,再排序
const arr = [1,3,2,[51,36,[2,-8,[236,777]]],[23,6,-1],[11,3,123,168,[36,-263,123]]]
// #1 扁平化后: [1, 3, 2, 5, 36, 2, -8, 236, 777, 23, 6, -1, 11, 3, 123, 168, 36, -263, 123]
console.log(arr.flat(Infinity))
// #2 扁平化去重后: [1, 3, 2, 51, 36, -8, 236, 777, 23, 6, -1, 11, 123, 168, -263]
console.log([...new Set(arr.flat(Infinity))])
// #3 扁平化、去重、排序后: [-263, -8, -1, 1, 2, 3, 6, 11, 23, 36, 51, 123, 168, 236, 777]
console.log([...new Set(arr.flat(Infinity))].sort((a,b)=>a - b))