I ran into this problem. The solution is to keep an ordered array of keys in addition to the original object.
var objects = { "7": {"id":"7","name":"Hello"}, "3": {"id":"3","name":"World"}, ... } var order = [ "3", "7", ... ];
Now, if you need a second element, you can do this search:
var second_object = objects[order[1]];
The ECMA standard does not say anything about the order of elements in an object. And especially Chrome reorders keys when they look like numbers. Example:
var example = { "a": "a", "b": "b", "1": "1", "2": "2" };
if you print this in Chrome you will get something like:
{ 1: "1", 2: "2", "a": "a", "b": "b" };
It's a little sour .. but life.
You can use the Andy solution bundled as well, basically wrapping the two together in one object.
An alternative that I use a lot is a custom map function that allows you to specify the order in which an object moves. Typically, you will sort when you print your data to the user, so when you loop and create your table rows (for example), your iterator will pass the rows in the order that your sort function calls. I thought it was a good idea :)
The signature looks like this:
function map(object, callback, sort_function);
Usage example:
map(object, function (row) { table.add_row(row.header, row.value); }, function (key1, key2) { return object[key1] - object[key2]; });