I have an array with french strings let say: “égrener” and “exact” I would like to sort it such as égrener is the first. When I do:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:descriptor];
NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];
I get the é at the end of the list… What should I do?
Thanks
There’s a convenience method in
NSStringthat lets you do this type of sorting easily:NSString’s underlying comparison method (compare:options:range:locale:) gives you even more options of how sorting should be done.Edit: Here’s the long story:
First, define a comparison function. This one is good for natural string sorting:
Then, sort the array.
The array in the example contains some funny characters: Numbers, diacritical marks, and some characters from unicode range ff00. The last character type looks like an ASCII character but is printed in a different width.
The used comparison function handles all cases in human predictable way. The sorted array has the following order: