Skip to content

Instantly share code, notes, and snippets.

@SubZane
Created December 13, 2022 19:45
Show Gist options
  • Save SubZane/d552d3250004c35196327c43ae43b6d5 to your computer and use it in GitHub Desktop.
Save SubZane/d552d3250004c35196327c43ae43b6d5 to your computer and use it in GitHub Desktop.
genericSort
export interface InterfaceGenericSort<T> {
property: Extract<keyof T, string | number | Date>
isDescending: boolean
}
export function genericSort<T>(objectA: T, objectB: T, sorter: InterfaceGenericSort<T>) {
const result = () => {
if (objectA[sorter.property] > objectB[sorter.property]) {
return 1
} else if (objectA[sorter.property] < objectB[sorter.property]) {
return -1
} else {
return 0
}
}
return sorter.isDescending ? result() * -1 : result()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment