- (NSComparisonResult)compare:(Person *)otherObject { return [self.birthDate compare:otherObject.birthDate]; } NSArray *sortedArray; sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];
方法2: spa
NSSortDescriptor *sortDescriptor; sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"birthDate" ascending:YES] autorelease]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; NSArray *sortedArray; sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];
sortedArrayUsingDescriptorssortedArrayUsingDescriptors返回的是NSArray * ,若是對NSMutableArray進行排序,須要
mutableSortedArray=(NSMutableArray *)[mArray sortedArrayUsingDescriptors:xxx]; code