Mausimo
Mausimo

Reputation: 8168

NSArray sortedArrayUsingSelector keeps erroring with SIGBART

EDIT: Are you even able to use a custom selector with "sortedArrayUsingSelector"? I got this to work using [[ComicArray valueForKey:@"Name"] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

The Exception "Does not recognize selector" is thrown. Anyone know how to sort this? The ComicArray is filled with ComicDB which is a custom class that contains .Name. I am trying to sort based on ComicDB.Name.

//atempt1
NSArray * comicArray2 = [[ComicArray valueForKeyPath:@"ComicDB.Name"] sortedArrayUsingSelector:@selector(compareByName:)];

//atempt2       
NSArray * compicArray2 = [ComicArray sortedArrayUsingSelector:@selector(compareByName:)];

- (NSComparisonResult)compareByName:(id)other
{
    return [[self valueForKeyPath: @"ComicDB.Name"] caseInsensitiveCompare:[other valueForKeyPath: @"ComicDB.Name"]];
}

Thanks, -Mike

Upvotes: 0

Views: 2074

Answers (2)

Mausimo
Mausimo

Reputation: 8168

Jan 23 2011 I could not get this to work. Instead i used the iOS 4.0 sort method:

NSArray * myArraySorted = [ComicArray sortedArrayUsingComparator:^(id obj1, id obj2)
                          {
                              return [[obj1 valueForKey:@"Name"] caseInsensitiveCompare: [obj2 valueForKey: @"Name"]]; 
                              // return [[obj1 Name] caseInsensitiveCompare: [obj2 Name]];
                          }];

Upvotes: 1

ughoavgfhw
ughoavgfhw

Reputation: 39925

sortedArrayUsingSelector: performs the selector on the objects in the array to determine their order. The compareByName: method should be in the ComicDB class. The following implementation will work if you have accessors defined. If you don't, change Name to valueForKey:@"Name".

- (NSComparisonResult)compareByName:(ComicDB *)other {
    return [[self name] caseInsensitiveCompare:[other name]];
}

Upvotes: 3

Related Questions