Skip to content

Instantly share code, notes, and snippets.

@khle
Created January 14, 2022 07:23
Show Gist options
  • Save khle/db340071fe4a8cd145cdbd0ea6bcc09f to your computer and use it in GitHub Desktop.
Save khle/db340071fe4a8cd145cdbd0ea6bcc09f to your computer and use it in GitHub Desktop.
findMatchingAndMoveToFirst.ts
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