Skip to content

Commit 089fded

Browse files
committed
feat(sort): add sort function
1 parent 6f4868d commit 089fded

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

index.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
scanRight,
4949
scanRight1,
5050
slice,
51+
sort,
5152
split,
5253
sum,
5354
tail,
@@ -504,6 +505,15 @@ test("groupByIdentity", t => {
504505
);
505506
});
506507

508+
test("sort", t => {
509+
t.deepEqual(sort([2, 4, 3, 1]), [1, 2, 3, 4]);
510+
t.deepEqual(sort(["hello", "goodbye"]), ["goodbye", "hello"]);
511+
t.deepEqual(
512+
sort([-2, 4, -3, 1], (a, b) => Math.abs(a) - Math.abs(b)),
513+
[1, -2, -3, 4]
514+
);
515+
});
516+
507517
test("forEach", t => {
508518
const a = ["a", "b", "c"];
509519
let s = "";

index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,18 @@ export function shuffle<T>(array: ArrayLike<T>): T[] {
15121512
return result;
15131513
}
15141514

1515+
export function sort(array: ArrayLike<boolean>): boolean[];
1516+
export function sort(array: ArrayLike<number>): number[];
1517+
export function sort(array: ArrayLike<string>): string[];
1518+
export function sort<T>(array: ArrayLike<T>, comparator: Comparator<T>): T[];
1519+
export function sort<T>(array: ArrayLike<T>, comparator?: Comparator<T>): T[] {
1520+
return copy(array).sort(comparator ?? (defaultCompare as any));
1521+
}
1522+
1523+
export function sortFn<T>(comparator: Comparator<T>): (array: ArrayLike<T>) => T[] {
1524+
return array => sort(array, comparator);
1525+
}
1526+
15151527
export function forEach<T>(
15161528
array: ArrayLike<T>,
15171529
f: (element: T, index: number) => void

0 commit comments

Comments
 (0)