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.

2
  • 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". Commented Mar 1, 2012 at 17:17
  • Just set data as a key for object in dictionary, no? Commented Mar 1, 2012 at 17:20

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.