PDA

View Full Version : Sorting....help! [SOLVED]




LostSoul80
Jan 29, 2009, 12:12 PM
Hi all,
how can i sort an NSArray containing NSDictionaries, wich contain NSNumber objects as a key, ascending by the keys?

Thanks



lee1210
Jan 29, 2009, 01:16 PM
I would look into:
http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSArray_Class/Reference/Reference.html#//apple_ref/occ/instm/NSArray/sortedArrayUsingDescriptors:

I just re-read what you said... and it seems like you want to sort by the keys not the values, which seems strange. Can you clarify what the keys look like?

-Lee

Bakerman
Jan 29, 2009, 01:21 PM
Create a custom sort selector to do the comparison and use -[NSArray sortUsingSelector:]. It's probably not the fastest way to do it, but definitely the easiest.

LostSoul80
Jan 29, 2009, 02:00 PM
I would look into:
http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSArray_Class/Reference/Reference.html#//apple_ref/occ/instm/NSArray/sortedArrayUsingDescriptors:

I just re-read what you said... and it seems like you want to sort by the keys not the values, which seems strange. Can you clarify what the keys look like?

-Lee

Hi, thanks for reply.
I was already using NSSortDescriptors, but such in a wrong way - i was having troubles because i hadn't realized NSNumber has a compare: method... so i was trying to create a custom comparison method, wich had had to convert the nsnumber into an nsstring to finally do the comparison, by calling -[NSString compare:string options:NSNumericSearch]. This makes me laugh. Sorry for this topic.