Skip to content

Instantly share code, notes, and snippets.

@alii
Created November 14, 2021 15:12
Show Gist options
  • Save alii/c39f2f4a9febd06733ac12ad3df57ce5 to your computer and use it in GitHub Desktop.
Save alii/c39f2f4a9febd06733ac12ad3df57ce5 to your computer and use it in GitHub Desktop.
Sort an array of objects with known keys
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;
});
}
@alii
Copy link
Author

alii commented Nov 14, 2021

Usage: Untitled-1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment