Created
July 7, 2013 09:32
-
-
Save dezinezync/5942926 to your computer and use it in GitHub Desktop.
Sorting an Object by it's 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
var params = { | |
method: 'artist.getInfo', | |
api_key: '9bd9ee', | |
user_id: '3016', | |
format: 'json', | |
nojsoncallback: 1, | |
auth_token: '7215' | |
}; | |
function sortObject(o) { | |
var sorted = {}, | |
key, a = []; | |
for (key in o) { | |
if (o.hasOwnProperty(key)) { | |
a.push(key); | |
} | |
} | |
a.sort(); | |
for (key = 0; key < a.length; key++) { | |
sorted[a[key]] = o[a[key]]; | |
} | |
return sorted; | |
} | |
console.log(sortObject(params)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment