Sort an NSMutableArray

//Credit: https://stackoverflow.com/a/805589
//method 1: use custom compare method
- (NSComparisonResult)compare:(Person *)otherObject {
    return [self.birthDate compare:otherObject.birthDate];
}

NSArray *sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];

//method 2: use NSSortDescriptor
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"birthDate"
                                           ascending:YES];
NSArray *sortedArray = [drinkDetails sortedArrayUsingDescriptors:@[sortDescriptor]];

//method 3: use block
NSArray *sortedArray = [drinkDetails sortedArrayUsingComparator:^NSComparisonResult(Person* a, Person* b) {
    return [a.birthDate compare:b.birthDate];
}];

Reverse an NSMutableArray

//Note: it will return an NSArray
NSArray *result = [[myArray reverseObjectEnumerator] allObjects];

results matching ""

    No results matching ""