Feb 13, 2009

(Objective-C) How to sort an array

Here is an example to sort an array:

- (NSArray *) sortArray:(NSArray *)array :(BOOL)inAscending {
if (inAscending) {
return [array sortedArrayUsingSelector:@selector(compare:)];
}
else {
NSArray *ascendArray = [array sortedArrayUsingSelector:@selector(compare:)];
return [[ascendArray reverseObjectEnumerator] allObjects];
}
}

No comments: