The code is collected from http://www.cocoabuilder.com/archive/cocoa/44099-void-sortusingselector-sel-comparator.html
This article explains how to use sortUsingSelector and what parameter it takes.
> Hello;
> I am new to apple and ObjectiveC and started by playing around with
> NSArray and NSMutableArray to learn something about the native
> datatypes.
>
> The question I have How do I sort a NSArray using
> - (void)sortUsingSelector:(SEL)comparator
>
> What does (SEL)comparator mean??
> I am new to apple and ObjectiveC and started by playing around with
> NSArray and NSMutableArray to learn something about the native
> datatypes.
>
> The question I have How do I sort a NSArray using
> - (void)sortUsingSelector:(SEL)comparator
>
> What does (SEL)comparator mean??
It means that comparator is a method (or a message).
It should be a method like this one:
- (NSComparisonResult) comparator:(ClassInTheArray *) inObject
You can either write your own for your type of Object or use a Category
like for this one:
@interface NSString (InsensitiveSearch)
- (NSComparisonResult)compareInsensitive:(NSString *)aString;
@end
@implementation NSString(InsensitiveSearch)
- (NSComparisonResult)compareInsensitive:(NSString *)aString
{
return [self compare:aString options:NSCaseInsensitiveSearch];
}
@end
Sorting the array will be then done with this code:
[myArray sortUsingSelector:@selector(compareInsensitive:)];