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"
}
}
To guarantee order, like @Joseph said, it needs to be in an array:
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.