Created
May 30, 2022 11:59
-
-
Save employee451/d648f45e0e4879f75a6f18552ccc9337 to your computer and use it in GitHub Desktop.
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
type AcceptedArrayContents = string | { [key: string]: any } | |
/** | |
* Sorts an array alphabetically. If an object is provided, it sorts by the provided sortProperty. | |
* @param array | |
* @param sortProperty | |
*/ | |
const sortArrayAlphabetically = ( | |
array: AcceptedArrayContents[] = [], | |
sortProperty = '' | |
): any[] => { | |
return array.sort((a: AcceptedArrayContents, b: AcceptedArrayContents) => { | |
const textA = (typeof a === 'string' ? a : a[sortProperty]).toUpperCase() | |
const textB = (typeof b === 'string' ? b : b[sortProperty]).toUpperCase() | |
return textA.localeCompare(textB) | |
}) | |
} | |
export default sortArrayAlphabetically |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment