Created
January 8, 2021 14:24
-
-
Save anurag-roy/441b3194017ba53b305db89c7fac3a11 to your computer and use it in GitHub Desktop.
Sort Object by Keys (Alphabetically)
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
let inputObject: {[key: string]: string} = { | |
"ServerError": "Could not connect. Try again", | |
"WS_BLDNGMGMT_AssignOpportunitiesFailure": "Assignment Failed", | |
"WS_GEN_GenericError": "Some Error Occurred", | |
"WS_SCRCRDMGMT_CriteriaCodeAlreadySelected": "Criteria with same Criteria Code already Selected", | |
}; | |
let sortedKeys: string[] = Object.keys(inputObject).sort((a,b) => a.localeCompare(b)); | |
let resultObject: {[key: string]: string} = {}; | |
for (let key of sortedKeys) { | |
resultObject[key] = inputObject[key]; | |
} | |
console.log(resultObject); | |
/** Output | |
{ | |
"ServerError": "Could not connect. Try again", | |
"WS_BLDNGMGMT_AssignOpportunitiesFailure": "Assignment Failed", | |
"WS_GEN_GenericError": "Some Error Occurred", | |
"WS_SCRCRDMGMT_CriteriaCodeAlreadySelected": "Criteria with same Criteria Code already Selected" | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment