Created
August 25, 2018 13:27
-
-
Save cobysy/306df411928b63f920b322dd50a857bf to your computer and use it in GitHub Desktop.
TypeScript orderBy
This file contains 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
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