Here is a function I am working with:
export const sortAlphabetically = <T>(array: T[], property: string) =>
array.sort((a: T, b: T) =>
a[property].localeCompare(b[property]));
The property
should be a valid key in T (as a string), and no other values should be accepted. I attempted to use property: [key in t]
, but it did not work as expected.
Is there a different approach I can take to achieve this?