Created
January 14, 2022 07:23
-
-
Save khle/db340071fe4a8cd145cdbd0ea6bcc09f to your computer and use it in GitHub Desktop.
findMatchingAndMoveToFirst.ts
This file contains 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
export interface Compare<T, U> { | |
compare: (t: T, u: U) => boolean | |
} | |
export function findMatchingAndMoveToFirst<T, U extends Compare<T, U>>( | |
list: readonly T[], | |
shouldbeFirst: U | |
): T[] { | |
return list.reduce( | |
(accumulator: T[], current: T) => | |
shouldbeFirst.compare(current, shouldbeFirst) | |
? [current, ...accumulator] | |
: [...accumulator, current], | |
[] | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment