1

I went through lots of questions here on SO (like this one) but I still need some assistance.

I need my sqlite select to order by slovenian alphabet (letter č comes after c, letter š after s and letter ž after z).

Here is the code I use:

static int sqlite3SloCollate(void * foo, int ll, const void *l, int rl,
                                        const void *r){
    NSString *left = [NSString stringWithCharacters:l length:ll];
    NSString *right = [NSString stringWithCharacters:r length:rl];
    //THIS IS WHERE I DON'T KNOW HOW TO COMPARE CHARACTERS
    NSComparisonResult rs = [left compare:right options:NSForcedOrderingSearch];

    return rs;
}

sqlite3_create_collation(database, "SLOCOLLATE", SQLITE_UTF8, NULL, &sqlite3SloCollate);

querySQL = [NSString stringWithFormat: @"SELECT s.id FROM lyrics l INNER JOIN song s ON (l.idSong=s.id) WHERE content LIKE '%%%@%%' GROUP BY s.id ORDER BY s.title COLLATE SLOCOLLATE;",searchString];

Which NSOrdering type should I use? Or do I have to write my own compare function (can you give me an example)?

2 Answers 2

1

I think that this function might help you :

- (NSComparisonResult)compare:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)range locale:(id)locale

(From Apple documentation).

You can create a locale using :

- (id)initWithLocaleIdentifier:(NSString *)string

(From Apple NSLocale Class Documentation).

This code should do the trick :

NSRange range = NSMakeRange(0, [left length]);
id locale = [[NSLocale alloc] initWithLocaleIdentifier:@"sl_SI"];
NSComparisonResult rs = [left compare:right options:NSForcedOrderingSearch range:range locale:locale];

I hope this will help.

1
  • Ty DCMaxxx! I had to change my code so left and right are initiated with stringWithUTF8String. The code now works for the most part but sometimes there are still some mistakes. I also noticed that when I NSLog strings I compare (left and right) they have additional characters that cannot be copy/pasted (+ and the ? character upside down...). Maybe I should add another column and order by that column! =)
    – ljubiccica
    May 29, 2013 at 9:43
1

The @DCMaxxx answer has most of it. Plus the comment that you need to use stringWithUTF8String. But there's some more issues.

1) stringWithUTF8String uses null-terminated c-strings, whilst sqlite is suppling strings with just a length and no null termination.

2) For the number of characters to compare, we need to take the shortest length, not just the left length.

3) When the comparison is equal for the compare, we then need to consider which string is longer.

Full code here. I use an NSMutableData object to convert length coded strings to null terminated strings. It's probably quicker and easier to do it with straight c code, if you are that way inclined.

static int sqlite3SloCollate(void * foo, int ll, const void *l, int rl,
                             const void *r){
    NSMutableData* ld = [NSMutableData dataWithBytes:l length:ll+1];
    [ld resetBytesInRange:NSMakeRange(ll, 1)];
    NSString *left = [NSString stringWithUTF8String:[ld bytes]];

    NSMutableData* rd = [NSMutableData dataWithBytes:r length:rl+1];
    [rd resetBytesInRange:NSMakeRange(rl, 1)];    
    NSString *right = [NSString stringWithUTF8String:[rd bytes]];

    NSRange range = NSMakeRange(0, MIN([left length],[right length]));
    id locale = [[NSLocale alloc] initWithLocaleIdentifier:@"sl_SI"];
    NSComparisonResult result = [left compare:right options:0 range:range locale:locale];

    if (result==NSOrderedSame) {
        if (ll>rl) {
            result = NSOrderedDescending;
        } else if (ll<rl) {
            result = NSOrderedAscending;
        }
    }    
    // NSLog(@"Comparison:%@ - %@ - %li",left,right,(long)result);
    return result;
}
1
  • Playing with this some more... This is still a flawed solution. Characters with accents such as 'ê' might appear in Unicode as a single character or a collation of the letter and the diacritic. Which means for a word that should be identical, the length will be different. Also this function is called very often, so creating those temporary NSData objects is bad. Definitely do the conversion to null-terminated string with C code. Jul 31, 2014 at 20:42

Your Answer

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

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