Sort an NSMutablearray using custom objects in Objective C

Solution:

Sort an NSMutablearray having two types.One is NSComparison Method and another one is NSSortdescriptor method.

NSComparison Method:
- (NSComparisonResult)compareArray:(NewMember *)otherObject {
    return [self.birthDate compare:otherObject.birthDate];
}

NSArray *sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compareArray:)];
The array will be passed to another method with custom objects and return the greater object.

NSSortdescriptor method:

NSSortDescriptor *sortDesc;
sortDesc = [[NSSortDescriptor alloc] initWithKey:@"birthDate"
                                           ascending:YES];
NSArray *sortedArray = [drinkDetails sortedArrayUsingDescriptors:@[sortDesc]];

The sortdescriptor method will be efficient method compare to previous one.

Comments

Popular posts from this blog

store cgpoint in userdefaults iOS swift

libc++abi: terminating due to uncaught exception of type NSException iOS swift