2

I have a NSMutableDictionary created from a JSON query, running the json query in a browser has the output ordered alphabetically as i need it, and displaying this in the correct order using NSLOG verifies this. However, when I populate the UITableView cells the order comes out completely differently yet I wish to preserve the original order.

I understand a dictionary is not designed to be ordered and i can map to a new sorted array however if i do this (if this is the right way to achieve this?) it's unclear how to address the right keys and index for the detail view. Any thoughts? Thanks.

The code for creating the JSON data and creating the table cells is below:

- (void) makeData {
    //Define dictionary
    fullDictionary = [[NSMutableDictionary alloc] init];

    //parse JSON data from a URL into an NSArray
    NSString *urlString = [NSString stringWithFormat:@"http://<JSON feed goes here>"];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSError *error;
    fullDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // cell data - extracting the appropriate values by object rows
    cell.textLabel.text = [[[fullDictionary allValues] valueForKeyPath:@"strTerm"] objectAtIndex:indexPath.row];

    return cell;
}
Fogmeister
  • 76,236
  • 42
  • 207
  • 306

1 Answers1

1

Judging from your code, it looks like you know how to get the appropriate array from the dictionary, because you're apparently using an array to set your current values of cell.textLabel. So reverse engineering that code, it looks like the unsorted array is determined by:

NSArray *originalArray = [[fullDictionary allValues] valueForKeyPath:@"strTerm"];

Now you just need to sort that array. If it was a simple array of strings, you can do something as simple as:

NSArray *sortedArray = [originalArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

If you were dealing with a more complicated array of dictionary entries or something like that, there are permutations of the sort methods that give you great control. See here for a more complicated sort method as a way of sorting JSON results: Filtering UITableView from XML source. That's a different problem than yours, but maybe it gives you a sense of what you can do with sortedArrayUsingComparator.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thank you for the answer,i've successfully extracted the dictionary into a sorted array and displayed uitableview. Just left, is find selected row.. using IndexPath.row unfortunately reverts back to the original order. 'code' - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { //identify which cell is selected by assigning some sort of key NSIndexPath *indexPathTwo = [self.tableView indexPathForSelectedRow]; [segue.destinationViewController setDetailGlossaryTitle: [[[fullDictionary allValues] valueForKeyPath:@"strTerm"] objectAtIndex:indexPathTwo.row]]; } – Codecraft Studio Nov 29 '12 at 18:59
  • Yes that's correct I use indexPath.row at the moment however this resolves to a jumble of the dictionary again – Codecraft Studio Nov 29 '12 at 19:04
  • @CodecraftStudio Rather than looking up the title in the `fullDictionary` again in your `prepareForSegue`, look it up in the `sortedArray`. – Rob Nov 29 '12 at 19:04