I am using a REST based web service to get data. No matter what the structure of the JSON document, the NSDictionary gets populated the same way. I want the sorting preserved as
You shouldn't be using JSON dictionary to store things whose order matters. As http://json.org/ says
An object is an unordered set of name/value pairs.
The entries in an NSDictionary
have no inherent order; they are unsorted by definition. The same is true for the array returned by allValues
, as the documentation clearly says:
The order of the values in the array isn’t defined.
You will need to sort the array afterwards. If you want to keep the same sort order that is in the JSON source, you would have to parse the JSON data manually and retrieve the values from the dictionary one after another. Or, if you know how the JSON data is sorted, just apply the same sorting algorithm to the array returned by allValues
.