View Full Version : Sorting Objects (just for my understanding)

12-24-2011, 07:01 AM
I have an Object, containing an object, containing an object....etc.
So, it's an Object of objects. Not an Array of objects.
And now I want to sort this Object of objects.
Is there a "native" way in JavaScript to sort an object?
Or do I understand it correctly, that I first have to convert the Object to an Array, and than perform an array.sort ?

This question is just for my understanding of JavaScript, so I don't go into the wrong direction.
Tnx in advance for any explanation.

12-24-2011, 08:25 AM
Please see the following links:


Array.sort accepts a callback function that you can use to sort the values in an array...If the values in the array are objects, then you can reference the object properties in your callback function -- and sort by object properties.

So, if you want to use the built in Array.sort feature, you'll have to convert one or more of your objects into arrays.

Of course, I don't see why you couldn't write your own sorting function for objects, and perhaps add the function to the Object prototype.