I need to sort JavaScript objects by key.
Hence the following:
{ 'b' : 'asdsad', 'c' : 'masdas', 'a' : 'dsfdsfsdf' }
Would become:
{ 'a' : 'dsfdsfsdf', 'b' : 'asdsad', 'c' : 'masdas' }
I need to sort JavaScript objects by key.
Hence the following:
{ 'b' : 'asdsad', 'c' : 'masdas', 'a' : 'dsfdsfsdf' }
Would become:
{ 'a' : 'dsfdsfsdf', 'b' : 'asdsad', 'c' : 'masdas' }
recursive sort, for nested object and arrays
Guys I'm figuratively shocked! Sure all answers are somewhat old, but no one did even mention the stability in sorting! So bear with me I'll try my best to answer the question itself and go into details here. So I'm going to apologize now it will be a lot to read.
Since it is 2018 I will only use ES6, the Polyfills are all available at the MDN docs, which I will link at the given part.
Answer to the question:
If your keys are only numbers then you can safely use
Object.keys()
together withArray.prototype.reduce()
to return the sorted object:However if you are working with strings I highly recommend chaining
Array.prototype.sort()
into all of this:If someone is wondering what reduce does:
If needed here is the explanation for the one liner:
Why Sorting is a bit complicated:
In short
Object.keys()
will return an array with the same order as we get with a normal loop:Sidenote - you can use
Object.keys()
on arrays as well, keep in mind the index will be returned:But it is not as easy at shown by those examples, real world objects may contain numbers and alphabetical characters or even symbols (please don't do it).
Here is an example with all of them in one object:
Now if we use
Array.prototype.sort()
on the array above the output changes:Here is a quote from the docs:
You have to make sure that one of them returns the desired output for you. In reallife examples people tend to mix up things expecially if you use different information inputs like APIs and Databases together.
So what's the big deal?
Well there are two articles which every programmer should understand:
In-place algorithm:
So basically our old array will be overwritten! This is important if you want to keep the old array for other reasons. So keep this in mind.
Sorting algorithm
This shows that the sorting is right but it changed. So in the real world even if the sorting is correct we have to make sure that we get what we expect! This is super important keep this in mind as well. For more JavaScript examples look into the Array.prototype.sort() - docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
JavaScript objects1 are not ordered. It is meaningless to try to "sort" them. If you want to iterate over an object's properties, you can sort the keys and then retrieve the associated values:
Alternate implementation using
Object.keys
fanciness:1Not to be pedantic, but there's no such thing as a JSON object.
Just to simplify it and make it more clear the answer from Matt Ball
As already mentioned, objects are unordered.
However...
You may find this idiom useful:
You can then iterate through kv and do whatever you wish.
I transfered some Java enums to javascript objects.
These objects returned correct arrays for me. if object keys are mixed type (string, int, char), there is a problem.