Sorting NSDictionary

10,643

For your requirements the easiest way is to create a new array from the keys, sort that, then use the array to reference items from the original dictionary.

(Note myComparison is your own method that will compare two keys).

NSMutableArray* tempArray = [NSMutableArray arrayWithArray:[myDict allKeys]]; 
[tempArray sortedArrayUsingSelector:@selector(myComparison:)];

for (Foo* f in tempArray)
{
  Value* val = [myDict objectForKey:f];
}
Share:
10,643
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I was wondering if someone can show me how to sort an NSDictionary; I want to read it starting from the last entry, since the key is Date + Time and I want to be able to append it to an NSMutableString. I was able to read it using an enumerator but I don't get the results I want.

    Thanks