Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort NSArray containing åäö

Need help to sort NSArray that contains åäö (Swedish chars) I am using for the moment

[keyArray addObjectsFromArray:[[names allKeys]sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]]; 

But this is sort A Å Ä Ö

like image 347
Sebastian Avatar asked Aug 23 '11 13:08

Sebastian


1 Answers

This question is similar to the one I answered here Sort NSArray of NSStrings like Addressbook on iphone sort . You need to do a diacritic insensitive search.

NSArray *array = [NSArray arrayWithObjects:@"aar", @"åäö", @"aao", nil];

NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    return [(NSString*)obj1 compare:obj2 options:NSDiacriticInsensitiveSearch|NSCaseInsensitiveSearch];
}];

Note: sortedArrayUsingComparator: requires iOS 4.0 and above. For iOS < 4.0 use sortedArrayUsingSelector:.

like image 118
Joe Avatar answered Oct 31 '22 12:10

Joe