Created
November 14, 2021 15:12
-
-
Save alii/c39f2f4a9febd06733ac12ad3df57ce5 to your computer and use it in GitHub Desktop.
Sort an array of objects with known keys
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
export function mapOrder<T, Key extends keyof T>(array: T[], order: Array<T[Key]>, key: Key) { | |
return array.sort((a, b) => { | |
const A = a[key]; | |
const B = b[key]; | |
if (order.indexOf(A) > order.indexOf(B)) { | |
return 1; | |
} | |
return -1; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: