Skip to content

Instantly share code, notes, and snippets.

@naxty
Created August 8, 2020 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naxty/b4dc085ce1fcf3e189cd9ba939dffbb9 to your computer and use it in GitHub Desktop.
Save naxty/b4dc085ce1fcf3e189cd9ba939dffbb9 to your computer and use it in GitHub Desktop.
Generic function to sort array with objects in TypeScript alphabetically.
const sort = <Element extends {name: string}>(array: Array<Element>) => {
return array.sort((a: Element, b: Element) => {
if (a.name < b.name){
return -1;
}
if (a.name > b.name){
return 1;
}
return 0;
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment