Created
December 13, 2022 19:45
-
-
Save SubZane/d552d3250004c35196327c43ae43b6d5 to your computer and use it in GitHub Desktop.
genericSort
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 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