Search code examples
objective-cnssortdescriptor

NSSortDescriptor - sort parent nsdictionnary by count of one particular child array


I have a main parent NSDictionnary which contains multiple objects, each containing multiple values. I want to sort the whole NSDictionnary based on the number of elements in one particular array (every object in the dictionnary contains this array).

Basically, if my dictionnary contains 3 objects, each having a name, an address, and an array containing the names of all the friends they have, I would like to sort the dictionnary based on the number of friends each of them have, without having to add an additional element to each object (e.g the number of friends).

I have tried something like:

descriptor = [NSSortDescriptor sortDescriptorWithKey:@"friends.count" ascending:NO];

But this doesn't work. I have also seen the idea:

descriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"@count" ascending:NO];

But this only seems to work if the parent array contains only one child array.

How can I achieve this?


Solution

  • You can't sort a dictionary. If you want to sort an array of dictionaries, use friends.@count.