The data I am sending my page is encoded in JSON, parsed using Javascript then displayed in an HTML SELECT element using a loop. The data arrives already sorted, but I am having issues keeping the correct order when decoding the JSON string, which nullifies the sorting applied on the data.
Sample data: {"test":{"4":"first","5":"second","3":"third"}}
Using jQuery's JSON parser and Javascript's eval() function, I am getting the following results:
{"test":{"3":"third","4":"first","5":"second"}}
It is not possible to modify the format of the data and the keys ("4", "5", "3") must remain in the same order. The real data is much more complex, but this sample illustrates very well my issue.
How can I maintain the order of the JSON data when parsing it from Javascript?
Use an array if you want to keep the order. That should be the only way to maintain the order in javascript.