sortUsingSelector

September 5, 2006

After spending 4 days with the problem of sorting an array of NSDictionaries (!!) using every kind of method imaginable, I finally found the solution:

You can’t use sortUsingSelector because that operates on the object in the array (in this case an NSDictionary). That explains the countless (”[CFDictionary compareScores] selector not recognized etc…“) I was getting. Phew

So use sortUsingFunction instead. And as the guys at cocoadev so eloquently showed:

int someSort (id obj1, id obj2, void *context)

{

        return [[(NSDictionary *)obj1 objectForKey:(NSString *)context] compare:[(NSDictionary *)obj2 objectForKey:(NSString *)context]];

}

Where “context” is the NSDictionary key you want to sort by.