Sort Array of NSString By Custom Method
How would I go about sorting an array of NSString Objects by a custom method. For example I have a method
-(int)calculateValue:(NSString *)aString
I would like to sort an array by descending order based on the returned value from this method. So for example I have an array
NSMutableArray *array = [[NSMutableArr开发者_开发知识库ay alloc] initWithObjects:@"Apple", @"Bee", @"Super", nil];
Now if I call calculateValue for each of those strings it might return values such as.
"Apple" = 15, "Bee" = 21, and "Super" = 3. Now I'm trying to sort that array based on the returned value for each NSString from the calculateValue method. So then the sorted array would like like
[Bee, Apple, Super]
See -sortedArrayUsingSelector:. You'll pass in a selector like @selector(myComparisonMethod:)
. Your comparison method should return an NSComparisonResult like NSOrderedAscending, NSOrderedDescending, or NSOrderedSame.
精彩评论