I have built an object in PHP, used JSON_encode function and send it as a JSON string to my JS script via ajax. Then I convert it back to an object. The problem I am having is that I wanted to keep the object in the order that it was originally created in. Please see this picture of what the object looks like once I get it into JS:
When I created the object, it was sorted by the customer field alphabetically. The customer name starting with A would come first, B second, etc. As you can see, now, the first element of the object as customer starting with S. It looks like somehow it got automatically sorted by the key of the top-level object, which is an integer, so I understand why this happened.
So i want to do is re-sort this object so that all the sub-objects are sorted by the customer
field alphabetically. Is this possible? If so, how do I do it?
Thanks!
object
entry in anarray
; you could then sort thearray
by the customer name. You cannot rely on the iteration order of an object in JavaScript. – Dewarjson_array
) you could sort it by doing this:json_array.sort(function(a, b){return a.customer > b.customer? 1: -1;});
– Kentondata = $.map(data, function(k, v) { return [k]; });
– Bypass