I have a servlet which talks with the database then returns a list of ordered (ORDER BY time) objects. At the servlet part, I have
//access DB, returns a list of User objects, ordered ArrayList users = MySQLDatabaseManager.selectUsers(); //construct response JSONObject jsonResponse = new JSONObject(); int key = 0; for(User user:users){ log("Retrieve User " + user.toString()); JSONObject jsonObj = new JSONObject(); jsonObj.put("name", user.getName()); jsonObj.put("time", user.getTime()); jsonResponse.put(key, jsonObj); key++; } //write out out.print(jsonResponse);
From the log I can see that the database returns User objects in the correct order.
At the front-end, I have
success: function(jsonObj){ var json = JSON.parse(jsonObj); var id = 0; $.each(json,function(i,item) { var time = item.time; var name = item.name; id++; $("table#usertable tr:last").after('<tr><td>' + id + '</td><td width="20%">' + time + '</td><td>' + name + '</td></tr>'); }); },
But the order is changed.
I only noticed this when the returned list has large size (over 130 users).
I have tried to debug using Firebug, the "response tab" in Firebug shows the order of the list is different with the log in the servlet.
Did i do anything wrong?
EDIT: Example
{"0":{"time":"2011-07-18 18:14:28","email":"xxx@gmail.com","origin":"origin-xxx","source":"xxx","target":"xxx","url":"xxx"}, "1":{"time":"2011-07-18 18:29:16","email":"xxx@gmail.com","origin":"xxx","source":"xxx","target":"xxx","url":"xxx"}, "2": ,..., "143":{"time":"2011-08-09 09:57:27","email":"xxx@gmail.com","origin":"xxx","source":"xxx","target":"xxx","url":"xxx"} ,..., "134":{"time":"2011-08-05 06:02:57","email":"xxx@gmail.com","origin":"xxx","source":"xxx","target":"xxx","url":"xxx"}}