»
S
I
D
E
B
A
R
«
Sort an NSMutableArray with custom objects
December 6th, 2009 by admin

To sort an NSMutableArray with custom objects, you can either implement a compare-method for your object:

- (NSComparisonResult)compare:(id)otherObject {
    return [self.birthDate compare:otherObject];
}

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

or even better: (The default sorting selector of NSSortDescriptor is compare:)

NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"birthDate"
                                              ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];
int myInt = (int) myFloat;
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay



Similar Posts:
Leave a Reply

Powered by WP Hashcash