7

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 Å Ä Ö

Sebastian
  • 71
  • 2

2 Answers2

5

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:.

Community
  • 1
  • 1
Joe
  • 56,979
  • 9
  • 128
  • 135
0

As the comments below the question and the answer states; [NSString localizedCompare:] is the best way to get words with åäö sorted the right way.

As I understand it NSDiacriticInsensitiveSearch treats å and ä as a and ö as o and that is not what you want when sorting swedish.

sanna
  • 1,165
  • 1
  • 16
  • 26