1

I am having an NSMutableDictionary with, being filled dynamically. It looks like this:

{
    SomeKey = 6;
    AnotherKey = 2;
    JustAnotherKey = 28;
}

I need to sort this, so it will be like this one:

{
    JustAnotherKey = 28;
    SomeKey = 6;
    AnotherKey = 2;
}

Is there any way to achieve this? Thanks in advance.

Robbietjuh
  • 848
  • 1
  • 8
  • 22

3 Answers3

4

No, sorry!

An NSDictionary doesn't support sorting it's keys - you would have to do that yourself.

Get the keys array from your dictionary, sort that and then go through and get the values from your dictionary. Something like :

NSArray *keys = [myDictionary allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingSelector:@selector(compare:)];

NSArray *values = [NSMutableArray array];
for (id key in sortedKeys)
    [values addObject:[myDictionary objectforKey:key]];

Now, values are in the correct order.

However, that's quite a lot of work; if you want them sorted, I would look at storing them in an array to start with?

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • PS If you just want to reverse your array of keys instead of sorting it then this answer will help : http://stackoverflow.com/questions/586370/how-can-i-reverse-a-nsarray-in-objective-c – deanWombourne Jan 20 '12 at 22:14
  • (though you can't guarentee the initial order of keys so actually just reversing them probably won't do what you want!) – deanWombourne Jan 20 '12 at 23:07
  • I wanted to order the objects inside of the Dictionary. So, just like in the example, I don't care what order the keys are. In the code you provided, it sorts the keys and puts in an array. That won't work in my project, unfortunately. – Robbietjuh Jan 20 '12 at 23:25
  • You _can't_ order the keys in a dictionary. You could store two arrays, one of keys and one of values and keep them in sync if you really needed to? – deanWombourne Jan 20 '12 at 23:40
1

Instead of using a NSMutableDictionary, you might consider to use a NSMutableArray and populate it with your own model class which contains a property with the key string and a property with the value. NSMutableArray provides methods to sort these objects.

Is this helpful enough?

Jodocus
  • 151
  • 6
1

A Dictionary is an unordered set, which means it doesn't have any order of its elements.So even if you insert the first object as say "one":"first value", and then "two":"second value", when you iterate over the keys, you might get it in any random order(eg: "two' and then "one").

However, if all you want is the values in sorted order, you can iterate over all the keys, fetch the values and store it in an array, and then sort them.

NSArray *values=[myDict allValues];
NSMutableArray *sortedKeys=[[NSMutableArray alloc] init];
NSArray *sortedValues = [values sortedArrayUsingSelector:@selector(yourSelector)];
for (val in sortedArray){
   NSString *key=(NSString*)[[myDict allKeysForObject:val] objectAtIndex:0];
   [sortedKeys addObject:key]
}

This would be starter for getting first the values in sorted order, and then the corresponding keys. (It is not doing any error checks. So beware of OutOfIndex exceptions).
I am not sure how good would be the efficiency of this code be, coz allKeysForObject would be iterating over all the keys.

aqs
  • 5,632
  • 3
  • 24
  • 24
  • Any way to get the keys in the same order as the data, after being sorted? Looks pretty good, though :-) – Robbietjuh Jan 20 '12 at 23:10
  • There is indeed a way for doing that. I have edited the answer to include it. pls accept the answer if good enough – aqs Jan 20 '12 at 23:25
  • Thank you very much. I had to edit the code a bit, and it worked perfectly! Just what I want it to do! Sorts the objects, gets the keys, and puts it in the same order. – Robbietjuh Jan 20 '12 at 23:55