iOSDevil iOSDevil - 2 years ago 59
iOS Question

Best way to sort an NSArray of NSDictionary objects?

I'm struggling with trying to sort an array of dictionaries.

My dictionaries have a couple of values of interest, price, popularity etc.

Any suggestions?

Answer Source

Use NSSortDescriptor like this..

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"interest"  ascending:YES];
    stories=[stories sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
    recent = [stories copy];

stories is the array you want to sort. recent is another mutable array which has sorted dictionary values. Change the @"interest" with the key value on which you have to sort.

All the best

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download