Sort Objective-c Array containing objects containing arrays

Wow ... what a title. Therefore, I have an NSMutableArray that is assigned data received from an API call. I do not control the structure of the response of the API request, and I would like to sort the array in this format. (Of course, other solutions are welcome)

This is a stupid example (to keep things simple), but the array I want to sort can be represented something like this:

myObjects = 
array (
  0 => object (
    name => @ "Jack",
    favoriteNumbers => object (
      numbers => array (
        0 => 7,
        1 => 11,
        2 => 6
      )
    )
  ),
  1 => object (
    name => @ "Jill",
    favoriteNumbers => object (
      numbers => array (
        0 => 1,
        1 => 2,
        2 => 3
      )
    )
  ),
  3 => object (
    name => @ "Sally",
    favoriteNumbers => object (
      numbers => array (
        0 => 68,
        1 => 103,
        2 => 99
      )
    )
  )
)

I know that I can just sort by name with something like:

NSSortDescriptor * sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey: @ "name" ascending: NO] autorelease];
NSArray * sortDescriptors = [NSArray arrayWithObject: sortDescriptor];
NSArray * sortByDurationArray = [myObjects sortedArrayUsingDescriptors: sortDescriptors]; 
// the variable "myObjects" holds the array representation from above

But how would I sort the array by each personal 1st favorite number?

The desired result sorts the array in the following order: Jill, Jack, Sally; since Jill 1st favorite number is 1, Jack is 7, and Sally is 68.

+3
1

NSSortDescriptor s selector. , - - -compareFavoriteNumberToObject:, NSComparisonResult. - NSDictionary, .

+2

Source: https://habr.com/ru/post/1779068/


All Articles