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

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift