views:

128

answers:

2

I thought caseinsensitiveLocalizedCompare: would take care of this (that is not including "the" and "a" in the comparison) , but it does not.

(Also, In response to the first answer below, I understand that "case insensitive" part wouldn't help, but I thought that the "localized" part may help.

I can't find any options to do this and google is unusable since I am searching for "the" and "a".

I figured since this is very common, that something would exist in Cocoa.

I am implementing my own method, but figured there was a built in way to do this.

+1  A: 

When you wonder whether Cocoa supports something, it generally helps to look at the documentation—in this case, the list of all of the options NSString supports for comparisons.

As you implement this, don't forget to put the list of articles into a localized resource file inside your app bundle, so that localizers can provide lists of strippable articles in their own languages. Load that file on demand, and keep it around throughout the lifetime of your process. Alternatively, for some things (e.g., band names), it may be better to have a single file with all known articles.

Peter Hosey
I did read, but wondered if I was missing something that seems so basic. Thanks for the tips, I am already on my way to implementing this.
Corey Floyd
There's no public function to do that, no. However, Apple does implement this functionality. The most obvious usage is in iTunes. Dig around in the iTunes package, and you'll find localized files `SortPrefixes.plist`. That can give you a start (of slightly questionable morality) for your localized versions. :)
Matt B.
A: 

For completeness, below is my code used to solve sort my objects. Add this in a category on NSString.

- (NSComparisonResult)localizedCaseInsensitiveExcludeTheCompare:(NSString*)aString{

    NSString* firstString = [self stringByRemovingArticlePrefixes];
    aString = [aString stringByRemovingThePrefixes];

    return [firstString localizedCaseInsensitiveCompare:aString];    

}

- (NSString*)stringByRemovingThePrefixes{

    if([self length] < 5)
        return self; 

    NSString* aString = [self copy];

    if([[aString substringToIndex:4] doesContainString:@"The "]){

        aString = [aString substringFromIndex:4];
        aString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    }else if([[aString substringToIndex:4] doesContainString:@"the "]){

        aString = [aString substringFromIndex:4];
        aString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    }

    return [aString autorelease];
}
Corey Floyd