Created
May 23, 2025 08:41
-
-
Save tanishqmanuja/7691d9e4e32bb4611e1af9c0febd50ed to your computer and use it in GitHub Desktop.
Utility to Combine Comparators for easy sorting!
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
/* Helper Types */ | |
type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends ( | |
x: infer I | |
) => void | |
? I | |
: never; | |
/* Comparator */ | |
export type Comparator<T> = (a: T, b: T) => number; | |
export type CombinedComparator<T extends Comparator<unknown>[]> = Comparator< | |
UnionToIntersection<Parameters<T[number]>[0]> | |
>; | |
export const Comparator = { | |
combine<CInputs extends Comparator<any>[]>( | |
...comparators: CInputs | |
): CombinedComparator<CInputs> { | |
return (a, b) => | |
comparators.reduce((result, comparator) => { | |
return result !== 0 | |
? result | |
: (comparator as CombinedComparator<CInputs>)(a, b); | |
}, 0); | |
}, | |
from<T, K extends string | number | boolean>( | |
selector: (x: T) => K, | |
order: "asc" | "desc" = "asc" | |
): Comparator<T> { | |
return (a, b) => { | |
const aKey = selector(a); | |
const bKey = selector(b); | |
let result: number; | |
if (typeof aKey === "string" && typeof bKey === "string") { | |
result = aKey.localeCompare(bKey); | |
} else { | |
result = aKey < bKey ? -1 : aKey > bKey ? 1 : 0; | |
} | |
return order === "asc" ? result : -result; | |
}; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment