Sort Array with Special Characters - iPhone

I have an array with French lines, say: "égrener" and "exact". I would like to sort it, for example, égrener is the first. When I do this:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:descriptor];
NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];

I get é at the end of the list ... What should I do?

thank

+2
source share
1 answer

This is a convenient method in NSStringwhich makes this type of sorting easy:

NSArray *sortedArray = [myArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

NSStrings basic comparison method ( compare:options:range:locale:) gives you even more sorting options.

Edit: Here is a long story:

First define a comparison function. This is useful for naturally sorting strings:

static NSInteger comparator(id a, id b, void* context)
{
    NSInteger options = NSCaseInsensitiveSearch
        | NSNumericSearch              // Numbers are compared using numeric value
        | NSDiacriticInsensitiveSearch // Ignores diacritics (â == á == a)
        | NSWidthInsensitiveSearch;    // Unicode special width is ignored

    return [(NSString*)a compare:b options:options];
}

Then sort the array.

    NSArray* myArray = [NSArray arrayWithObjects:@"foo_002", @"fôõ_1", @"fôõ_3", @"foo_0", @"foo_1.5", nil];
    NSArray* sortedArray = [myArray sortedArrayUsingFunction:comparator context:NULL];

: , unicode ff00. ASCII, .

. :

oo_0
fôõ_1oo_1.5
foo_002
fôõ_3
+5

All Articles