Konrad Kolasa
Konrad Kolasa

Reputation: 621

Sort NSArray of NSDictionaries

I have a problem with sorting... I need to sort NSArray containing NSDictionaries. As an order key I have to use element from the NSDictionary.

I was reading a little about sorting NSArray but it is not clear to me. Could you try help me?

Upvotes: 1

Views: 373

Answers (1)

Rob Napier
Rob Napier

Reputation: 299265

Use an NSSortDescriptor and a keypath. Assuming the key for the dictionary is key:

NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"key" ascending:YES];
NSArray *sorted = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sd]];

Upvotes: 1

Related Questions