Created
April 8, 2015 10:42
-
-
Save kalpeshhpatel/7696aca9361562b04932 to your computer and use it in GitHub Desktop.
Javascript: Sort Object By Keys
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
function sortObject(o) { | |
var sorted = {}, a = [],key; | |
//Get array keys | |
a = Object.keys(o); | |
//Sort it | |
a.sort(); | |
//Construct object by iteration | |
for (key = 0; key < a.length; key++) { | |
sorted[a[key]] = o[a[key]]; | |
} | |
return sorted; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment