Created
October 6, 2020 12:26
-
-
Save DScheglov/0f7e158da73ef29f87afcb563587a578 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ComparatorFn<T> = (a: T, b: T) => number; | |
type CasterFn<T> = (value: T) => number; | |
export const reverse = <T>(compare: ComparatorFn<T>): ComparatorFn<T> => (a: T, b: T) => -compare(a, b); | |
const combineComparators = <T>(compareA: ComparatorFn<T>, compareB: ComparatorFn<T>): ComparatorFn<T> => | |
(a: T, b: T) => compareA(a, b) || compareB(a, b); | |
const voidComparator = <T>(a: T, b: T): number => 0; | |
export const combinedComparator = <T>(...comparators: ComparatorFn<T>[]): ComparatorFn<T> => | |
comparators.length > 0 | |
? comparators.reduce(combineComparators) | |
: voidComparator; | |
const sortBy = <T>(caster: CasterFn<T>): ComparatorFn<T> => (a: T, b: T) => caster(a) - caster(b); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment