How do I sort an NSMutableArray with custom objects in it?

前端 未结 27 3260
予麋鹿
予麋鹿 2020-11-21 04:45

What I want to do seems pretty simple, but I can\'t find any answers on the web. I have an NSMutableArray of objects, and let\'s say they are \'Person\' objects

27条回答
  •  一个人的身影
    2020-11-21 05:07

    There is a missing step in Georg Schölly's second answer, but it works fine then.

    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"birthDate"
                                                  ascending:YES] autorelease];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *sortedArray;
    sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];
    

    // added the 's' because time was wasted when I copied and pasted and it failed without the 's' in sortedArrayUsingDescriptors

提交回复
热议问题