Created
September 19, 2013 20:45
-
-
Save kristerkari/6629600 to your computer and use it in GitHub Desktop.
Sort JS object by keys: https://twitter.com/rstacruz/status/380786914795585537
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
function sortByKey(obj) { | |
var newObj = {}; | |
Object.keys(obj).sort().forEach(function(key) { | |
newObj[key] = obj[key]; | |
}); | |
return newObj; | |
} | |
var obj = { | |
b: 2, | |
a: 1, | |
c: 3 | |
}; | |
sortByKey(obj); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment