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:
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
Post a Comment