如何按数组计数对NSArray的嵌套NSArray进行排序?

我有一个包含嵌套NSArrays的NSArray。 我正在寻找一种方法来按照嵌套数组的对象数按升序对父数组进行排序。 所以如果[array1 count]是4, [array2 count]是2而[array3 count]是9,我会得到:array2,array1,array3 ……

有一些解决方案,其中之一是:

 NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"@count" ascending:YES]; NSArray *sds = [NSArray arrayWithObject:sd]; NSArray *sortedArray = [array sortedArrayUsingDescriptors:sds]; 
 static NSInteger MONSortObjectsAscendingByCount(id lhs, id rhs, void* ignored) { /* error checking omitted */ const NSUInteger lhsCount = [lhs count]; const NSUInteger rhsCount = [rhs count]; if (lhsCount < rhsCount) { return NSOrderedAscending; } else if (lhsCount > rhsCount) { return NSOrderedDescending; } else { return NSOrderedSame; } } /* use if mutable, and you wnat it sorted in place */ - (void)sortUsingFunction:(NSInteger (*)(id, id, void *))compare context:(void *)context; /* else use */ - (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))compare context:(void *)context;