Created
September 25, 2019 11:16
-
-
Save vijaydeepak-tt/824d65caf90a5dc3231f2322d60c5b09 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| sortArrObjects( | |
| key: string, | |
| order: string = 'asc', | |
| isDate: boolean = false | |
| ) { | |
| return (a, b) => { | |
| if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) { | |
| // if property doesn't exist on either object | |
| return 0; | |
| } | |
| let varA; | |
| let varB; | |
| if (isDate) { | |
| varA = new Date(a[key]); | |
| varB = new Date(b[key]); | |
| } else { | |
| varA = typeof a[key] === 'string' ? a[key].toUpperCase() : a[key]; | |
| varB = typeof b[key] === 'string' ? b[key].toUpperCase() : b[key]; | |
| } | |
| let comparison = 0; | |
| if (varA > varB) { | |
| comparison = 1; | |
| } else if (varA < varB) { | |
| comparison = -1; | |
| } | |
| return order === 'asc' ? comparison * -1 : comparison; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment