Question

It somehow does not work with Scandinavian alphabets.

When I enter @"å", and there are two strings that "sandwich" that search word; one string is starting with "ø" which is lower than "å" and another string that starts with "å". However this comparison code returns SOMETIMES correct, SOMETIMES wrong result, even though I used same search string, i.e. @"å" over and over again. Is there something I have overlooked?

EDIT: sorry it should be two different compStrings (as in my code), I forgot to put number 2 as suffix.

compString2 means the second string that begins with å. compString is the one that begins with ø.

I need to compare search string with two strings to find out whether it is between these two strings or not.

Let me formulate it in a better way, for those that don't know Scandinavian alphabets, å is like c and ø is like b, and if search string is c, then it must be between b and c, right?

Here is my code:

NSString *compString = [array objectAtIndex:i]; //i is an incremental number from a for-loop

//norwegian language
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"no_NO"];


if([searchString compare:compString2
                 options:(NSCaseInsensitiveSearch | NSAnchoredSearch)
                   range:range
                  locale:locale] == (NSOrderedAscending | NSOrderedSame)) {

    if([searchString compare:compString
                     options:(NSCaseInsensitiveSearch | NSAnchoredSearch) 
                       range:range
                      locale:locale] == NSOrderedDescending) {

        NSLog(@"found");

    }
}

FINAL-EDIT:

I found the problem. The solution is simple. NEVER DO what I wrote bitwise OR operator "|" or plain OR operator "||".

if([searchString compare:compString
options: NSCaseInsensitiveSearch 
range:range locale:locale] == NSOrderedAscending ||
 [searchString
 compare:compString
 options: NSCaseInsensitiveSearch 
range:range locale:locale] == NSOrderedSame)

meaning searchString <= compString

if([searchString
   compare:compString
   options: NSCaseInsensitiveSearch
    range:range locale:locale] == NSOrderedDescending)

meaning searchString > compString

Was it helpful?

Solution

I think you should not use NSOrderedAscending and NSOrderedSame as bits of a bit field in the first comparison result evaluation, since you do not get the logical (!) OR that you probably want.

Try this instead in your first if ():

if([searchString compare:compString2
                 options:(NSCaseInsensitiveSearch | NSAnchoredSearch)
                   range:range
                  locale:locale] != NSOrderedDescending) {

    /* ... */

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top