Skip to content

Instantly share code, notes, and snippets.

@cobysy
Created August 25, 2018 13:27
Show Gist options
  • Save cobysy/306df411928b63f920b322dd50a857bf to your computer and use it in GitHub Desktop.
Save cobysy/306df411928b63f920b322dd50a857bf to your computer and use it in GitHub Desktop.
TypeScript orderBy
declare global {
interface Array<T> {
sortBy(selector: (elem: T) => any): T[];
}
}
if (!Array.prototype.sortBy) {
Array.prototype.sortBy = function <T>(selector: (elem: T) => any): T[] {
return this.sort((a, b) => {
return selector(a) < selector(b) ? -1 : 1;
});
}
}
// Example
// let data = [{ name: 'Ray' }, { name: 'Albert' }];
// console.log(data.sortBy(x => x.Name));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment