-2

I try to sort my responseObject alphabetically get by AFNetworking. My request return this:

({
    id = 1;
    name = "& Other Stories";
},
    {
    id = 2;
    name = 0044;
},
    {
    id = 3;
    name = "1 et 1 Font 3";
})

I need to sort this alphabetically for use indexSectionTitles in my tableView. I want to sort my dictionary like this:

brands = @{@"A" : @[@"Afdlskh", @"Adslkqhd", @"Afdslcxw"],
            @"B" : @[@"Bfdlskh", @"Bdlsk"],
            @"C" : @[@"Dvlfkd", @"Dlfkh"],…
Cœur
  • 37,241
  • 25
  • 195
  • 267
fl0_9
  • 261
  • 3
  • 12

1 Answers1

1

NSDictionary objects cannot be ordered, so you need to keep a separate NSArray of the dictionary keys and order that as desired (you are probably not interested in ordering the dictionary keys in alphabetical order, rather the order needs to reflect whatever dictionary value is important to you).

When populating the tableview, via its datasource methods, you retrieve the key from the array, using the index given, and then use that key to access the data in the dictionary.

If this all seems too daunting then you probably want to create a custom object for each dictionary element and keep an array of those around. Using a custom object is normally always the best thing to do, rather then keeping the dictionary, which should be considered merely a transport data structure and nothing more.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242