Skip to content

Instantly share code, notes, and snippets.

@kalpeshhpatel
Created April 8, 2015 10:42
Show Gist options
  • Save kalpeshhpatel/7696aca9361562b04932 to your computer and use it in GitHub Desktop.
Save kalpeshhpatel/7696aca9361562b04932 to your computer and use it in GitHub Desktop.
Javascript: Sort Object By Keys
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