0

My json object looks like this:

events:{ "2": { "action": "some value", "property": "some value" } "0": { "action": "some value", "property": "some value" } "1": { "action": "some value", "property": "some value" } } 

I need to sort the properties of the object. Is there anyway to do this? The result should be like this:

events:{ "0": { "action": "some value", "property": "some value" } "1": { "action": "some value", "property": "some value" } "2": { "action": "some value", "property": "some value" } } 
5
  • 4
    in objects, order is not guaranteed. store them in arrays instead. Commented Apr 14, 2012 at 7:08
  • 2
    @Joseph — That should be an answer. :) Commented Apr 14, 2012 at 7:09
  • 1
    @Srikanth Kshatriy — That is an object literal, not JSON. JSON is a data serialisation format. Commented Apr 14, 2012 at 7:10
  • 1
    This looks like a good candidate for an XY Problem: mywiki.wooledge.org/XyProblem. What are you really trying to do? Commented Apr 14, 2012 at 7:11
  • @joeframbach yeah.. looks like a XY Problem but was looking for X's solution only :) Commented Apr 14, 2012 at 7:32

2 Answers 2

1

To guarantee order, like @Joseph said, it needs to be in an array:

var events = { "2": { "action": "some value", "property": "some value" }, "0": { "action": "some value", "property": "some value" }, "1": { "action": "some value", "property": "some value" } }; var eventOrder = []; for( var prop in events ){ if( events.hasOwnProperty( prop ) ){ eventOrder.push( prop ); } } eventOrder.sort(); 

Note: this is going to sort by ALPHABETICAL order since you're using strings, so if you have more than 9 items, you may end up with eventOrder as [ "1", "10", "2", ... ]. If this is not desired, you could parseInt before storing in the array, and the force to string later when you're doing lookups.

Sign up to request clarification or add additional context in comments.

4 Comments

This suited best for my requirement. Thanks @webnesto
There is no need to "force" a string later, alert(events[2]) will work fine.
@RobG - you're technically right, but you're relying on JS type conversion at that point. It is the same thing as using a "==" for evaluations and if you come down on the "===" side of the arguments there, then you'd also want to make sure you were using strings for your hash lookups.
Most do type conversion in javascript without giving it a second thought, it's a feature of the language that is there to be used. Does anyone do obj[String(a)] or obj[String(fn())] or even String(a) + String(b)? FWIW I only ever use === where it's needed, which is not often.
1

in conjunction to my comment, store it this way instead: use an array, then store those indexes as ids. then you can move them around using some sorting algorithm. here' are some samples of sorting algorithms you might want to take a look

events:[ { "id" : 2, "action": "some value", "property": "some value" }, { "id" : 0, "action": "some value", "property": "some value" }, { "id" : 1, "action": "some value", "property": "some value" } ] 

1 Comment

I generally prefer to store order and data as separate elements - { events: { "foo" : { "id" : "foo", ... } }, eventOrder: [ "foo", ... ] }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.