I made a XHR request on a server i got and it gives the right order when i go to the page on a browser put on android it gives back the wrong order.
i have something on date like 04-05-2012 and 05-05-2012 and the app gives back the wrong order like: 05-05-2012 and 04-05-2012 this isn't backwards because i have more then 2 items and those items don't come in a backwards order or in a normal order. (on iPhone it works well)
this is my code:
xhr.open('GET','http://somewhere.com/index.php/'+variable); xhr.send();
xhr.onload = function() { var result = JSON.parse(this.responseText); alert(result); // it gives back the wrong order here }
2 Answers
There is no guarantee of the order of keys in an object. While the de facto standard is to to order by insertion, it can't always be counted on, as some JavaScript engines handle it differently. Unfortunately, it is a matter of who is technically right vs what everybody else is doing.
The best thing you can do is to have the server put things that need to be ordered into an array, or if that is not possible, try to order them client side. Here is a SO discussion on the topic of object ordering along with an example for ordering that you may try to modify for your use.
As you've seen, Android, probably running the V8 JS engine, is ordering things differently thank the JS compiler on iPhone and other browsers. There are lots of discussions out there going on about the 'order of things' in V8 and other engines, including this one that is now in its fourth year.
Additionally, when sorting dates as you have mentioned in the question, you may also run into other issues with the date formatted like that. You may want to consider converting into a timestamp or other format via Date object in order to avoid issues with sorting them and other potential localization (or localisation?) issues.
this is the thing i do with my items(server side) this is in order and in this case stand_details is something like group A group B so that is to be ordered right? if so how exactly because the order function mentioned it the link you send wasn't working
$result[$item->stand_details][] = array( 'name' => $item->name, 'name_id' => $item->name_id, 'something' => $item->something );again this is in correct order on server side but gets displayed wrong.
thanks for all your help!
Your Answer
Think you can help? Login to answer this question!