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
| NSDiacriticInsensitiveSearch
| NSWidthInsensitiveSearch;
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, .
. :
foo_0
fôõ_1
foo_1.5
foo_002
fôõ_3