让我们假设我想要一个排序函数,它返回输入数组的排序副本。我很天真地试过
function sort(arr) {
return arr.sort();
}
我用这个测试了一下,结果显示我的排序方法改变了数组。
var a = [2,3,7,5,3,7,1,3,4];
sort(a);
alert(a); //alerts "1,2,3,3,3,4,5,7,7"
我也尝试过这种方法
function sort(arr) {
return Array.prototype.sort(arr);
}
但它根本不起作用。
有没有一种直接的方法来解决这个问题,最好是一种不需要手动滚动我自己的排序算法或将数组中的每个元素复制到一个新的数组中的方法?
您还可以扩展现有的Array功能。这允许将不同的数组函数链接在一起。
Array.prototype.sorted = function (compareFn) {
const shallowCopy = this.slice();
shallowCopy.sort(compareFn);
return shallowCopy;
}
[1, 2, 3, 4, 5, 6]
.filter(x => x % 2 == 0)
.sorted((l, r) => r - l)
.map(x => x * 2)
// -> [12, 8, 4]
typescript中相同:
// extensions.ts
Array.prototype.sorted = function (compareFn?: ((a: any, b: any) => number) | undefined) {
const shallowCopy = this.slice();
shallowCopy.sort(compareFn);
return shallowCopy;
}
declare global {
interface Array<T> {
sorted(compareFn?: (a: T, b: T) => number): Array<T>;
}
}
export {}
// index.ts
import 'extensions.ts';
[1, 2, 3, 4, 5, 6]
.filter(x => x % 2 == 0)
.sorted((l, r) => r - l)
.map(x => x * 2)
// -> [12, 8, 4]