I’m totally new to iPhone/OSX development and I’m trying to sort an NSMutableArray filled with objects in alphabetical order but I fail. Items starting with Å or Ä ends up with items starting with A and items starting with Ö ends up with items starting with O. I have tried different sorting methods that I have found here on Stackoverflow but none of them seems to work (for me at least)
NSSortDescriptor* stationDescriptor = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray* descriptors = [NSArray arrayWithObject:stationDescriptor];
[stations sortUsingDescriptors:descriptors];
[stations sortUsingComparator:^NSComparisonResult(Station* obj1, Station* obj2){
return [[obj1 name] localizedCaseInsensitiveCompare:[obj2 name]];
}];
Ok, progress! I have been successful in getting this to work by changing the language on the emulator to Swedish, is there any way I can get the same result when the phone is set to English? I know in C# you can send in a reference to a specific culture when sorting/comparing strings.
/Viktor
Looking at the docs for NSString gives you the answer.
You are using localizedCaseInsensitiveCompare: and the docs for this state
So jumping to the docs for compare:options:range:locale: you can figure it out. You’ll need something like