Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting NSDictionary from JSON for UITableView
    text
    copied!<p>I am having trouble trying to sort my NSDictionary in order, and it's been weeks and I still haven't figured it out, so I wish some one could give me a hand here...</p> <p>NSDictionary data is from JSON and the data is already sorted from the server and it displayed in order in the JSON, but when it retrieved and converted to NSDicitionary, the order is all wrong...</p> <p>This is the JSON</p> <pre><code>{ "categories":{ "unknownCategoryName":[ { "key1":"value1", "key2":"value2" }], "unknownCategoryName1":[ { "key1":"value1", "key2":"value2" }], "unknownCategoryName2":[ { "key1":"value1", "key2":"value2" }], "unknownCategoryName3":[ { "key1":"value1", "key2":"value2" }] } } </code></pre> <p>The category quantity and its name will not be known until the JSON is received, so this is what I am using to get the count and setting up the tableView and the section</p> <pre><code>NSDictionary *results = [jsonString JSONValue]; NSDictionary *all = [results objectForKey:@"categories"]; self.datasource = all; - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self.datasource count]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSString *title = [[self.datasource allKeys] objectAtIndex:section]; return title; } </code></pre> <p>And what I want to do is to list out the section and the section title as the order in the JSON.... rather than a chaos display like </p> <p>unknownCategory3<br> unknownCategory1<br> unknownCategory<br> unknownCategory2</p> <p>The "unknownCategory" is Product Category name, they are not in alphabet order, without any number, and they are already sorted and display in order in the JSON... </p> <p>So, it would be great if you guys could help. Thanks in advance...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload