Skip to content

Commit

Permalink
feat(sortbydescending): add sortByDescending function
Browse files Browse the repository at this point in the history
  • Loading branch information
djcsdy committed Jan 13, 2022
1 parent 51d7781 commit 524b670
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1528,15 +1528,23 @@ export function sortBy<T>(array: ArrayLike<T>, select: SortSelect<T>): T[] {
return sort(array, (a, b) => defaultCompare(select(a) as any, select(b) as any));
}

export function sortByFn<T>(select: SortSelect<T>): (array: ArrayLike<T>) => T[] {
return array => sortBy(array, select);
}

export function sortByDescending<T>(array: ArrayLike<T>, select: SortSelect<T>): T[] {
return sort(array, (a, b) => -defaultCompare(select(a) as any, select(b) as any));
}

export function sortByDescendingFn<T>(select: SortSelect<T>): (array: ArrayLike<T>) => T[] {
return array => sortByDescending(array, select);
}

export type SortSelect<T> =
| ((element: T) => boolean)
| ((element: T) => number)
| ((element: T) => string);

export function sortByFn<T>(select: SortSelect<T>): (array: ArrayLike<T>) => T[] {
return array => sortBy(array, select);
}

export function forEach<T>(
array: ArrayLike<T>,
f: (element: T, index: number) => void
Expand Down

0 comments on commit 524b670

Please sign in to comment.