0

I am having problem with NSString compare. The localizedCaseInsensitiveCompare: or any other compare: method in NSString is returning wrong result to me with extended ascii character set. for example below code, after comparison, noString should be after the enString1.

any idea, how can I solve the problem?

NSString * noString = @"ÅåÆæØøab"; // ASCII values, 197, 229, 198, 230, 216, 248, 97, 98

    NSString * enString1 = @"fbcd";
    NSString * enString2 = @"bbcd";
    NSString * enString3 = @"zbcd";

    NSLog(@"%i", [enString1 localizedCaseInsensitiveCompare:enString2]); // 1
    NSLog(@"%i", [enString1 localizedStandardCompare:noString]); // 1, should be -1
    NSLog(@"%i", [enString1 localizedCaseInsensitiveCompare:noString]); // 1, should be -1

"aa" compareTo "aå" is returning Ascending. Thats right. But,

"aa" compareTo "aæ" is returning Descending, why? same for "aø" too.

2 Answers 2

0

The following way can be solution, but it works for comparison with å but not for ø and æ.

NSLog(@"%i", [enString1 compare:noString options:NSCaseInsensitiveSearch 
                              range:NSMakeRange(0, [enString1 length])  
                             locale:[[NSLocale alloc] initWithLocaleIdentifier:@"no"] ]);
0

Find here More on string operations

If anybody stubmle upon this question, this is how I made a solution. It seems there is still some problem with non-english characters in NSString comparison. Specially for Å, Ø and Æ etc. The sorting order seems AÅ...ÆØ even with localized compare. THough ASCII code is different. So I wrote my own comparison method,

- (NSComparisonResult) caseInsensitiveStringCompare:(NSString *)string1 
                                      anotherString:(NSString *)string2 
{
    NSString * ciString1 = [string1 lowercaseString];
    NSString * ciString2 = [string2 lowercaseString];
    return [self stringCompare:ciString1 anotherString:ciString2];
}

- (NSComparisonResult) stringCompare:(NSString *)string1 
                          anotherString:(NSString *)string2
{
    int len1 = [string1 length];
    int len2 = [string2 length];

    if (len1 < len2) {
        int i = 0;

        while ( i < len1 && [string1 characterAtIndex:i] == [string2 characterAtIndex:i]  ) 
        {
            i++;
        }
        if (i == len1) {
            return NSOrderedAscending;
        } else if ([string1 characterAtIndex:i] < [string2 characterAtIndex:i]) {
            return NSOrderedAscending;
        } else {
            return NSOrderedDescending;
        }

    } else { // str1 >= str2 len
        int i = 0;

        while ( i<len2 && [string1 characterAtIndex:i] == [string2 characterAtIndex:i]) 
        {
            i++;
        }
        if (i == len2 && i == len1) {
            return NSOrderedSame;
        } else if (i == len2){
            return NSOrderedDescending;
        } else if ([string1 characterAtIndex:i] < [string2 characterAtIndex:i]) {
            return NSOrderedAscending;
        } else {
            return NSOrderedDescending;
        }
    }

    return NSOrderedSame;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.