0

I want a sorted NSDictionary, but the ways I've seen of accomplishing that seem too complicated.

What if I just do an NSArray of NSDictionary's? That way I know the order of iterated NSDictionary's. I need an order because I'm dealing with chronology (datas as days of month).

I still think there may be a better way so I'm turning to the pros (you guys).

Thanks.

edc1591
  • 10,146
  • 6
  • 40
  • 63
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
  • 1
    Not sure if I understand. You want to sort the contents of your NSDictionary into chronological order, is that correct? That means to say, you want to sort them in order of a key containing a data? An array of NSDictionaries wouldn't solve your problem. This would mean every element in your array would be an NSDictionary. What would each contain? How could it be sorted? I could be misunderstanding you but it's I can't make sense of an "array of dictionaries". – wmorrison365 Mar 01 '12 at 17:17
  • Just set data as a key for object in dictionary, no? – Roman Temchenko Mar 01 '12 at 17:20

2 Answers2

2

Presuming you want to sort the keys of your NSDictionary, see this SO post:

Sort an NSMutableDictionary

Note it just takes the keys from the dictionary (as an array) and sorts the array. You can then use the sorted keys list to access the dictionary entries in order.

Community
  • 1
  • 1
wmorrison365
  • 5,995
  • 2
  • 27
  • 40
0

I haven't tried this but the NSDictionaryController supports sorting. There is an example at this link.

The OP wishes to sort by key in one case and sort by value in another. The answer to his questions is in this form:

NSSortDescriptor *caseInsensitiveDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"key" ascending: YES selector: @selector(caseInsensitiveCompare:)] autorelease];
   [dictionaryController setSortDescriptors: [NSArray arrayWithObject: caseInsensitiveDescriptor]];    

NSDictionaryController inherits from NSArrayController, and probably implements the array like you were considering, but it does it transparently (you don't have to write the code).

Jim
  • 5,940
  • 9
  • 44
  • 91