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"
}

}
Srikanth Kshatriy
  • 444
  • 1
  • 4
  • 10

2 Answers2

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"
    }
]
Joseph
  • 117,725
  • 30
  • 181
  • 234
  • I generally prefer to store order and data as separate elements - { events: { "foo" : { "id" : "foo", ... } }, eventOrder: [ "foo", ... ] } – webnesto Apr 14 '12 at 07:20
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.

webnesto
  • 1,319
  • 8
  • 7
  • This suited best for my requirement. Thanks @webnesto – Srikanth Kshatriy Apr 14 '12 at 07:31
  • There is no need to "force" a string later, `alert(events[2])` will work fine. – RobG Apr 14 '12 at 08:11
  • @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. – webnesto Apr 14 '12 at 14:58
  • 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. – RobG Apr 15 '12 at 06:34