I have an array of strings (names)
and i would like to sort them like how the address book on the iphone sorts them
any suggestions?
You need to perform a diacritic insensitive comparison against the strings. NSString
has a compare:options:
method with an option of NSDiacriticInsensitiveSearch
.
NSArray *array = [NSArray arrayWithObjects:@"éli", @"bob", @"earl", @"allen", @"àli", @"aaron", nil];
NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [(NSString*)obj1 compare:obj2 options:NSDiacriticInsensitiveSearch|NSCaseInsensitiveSearch];
}];
Edit:
Here is a full example that will section the results based on the first character diacritic insensitive. I put in a dictionary so you would need to keep track of the sorted keys on your own to display properly.
NSArray *array = [NSArray arrayWithObjects:@"éli", @"bob", @"earl", @"allen", @"àli", nil];
NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [(NSString*)obj1 compare:obj2 options:NSDiacriticInsensitiveSearch|NSCaseInsensitiveSearch];
}];
NSMutableDictionary *sectioned = [NSMutableDictionary dictionary];
NSString *firstChar = nil;
for(NSString *str in sorted)
{
//Ignore empty strings
if(![str length])continue;
NSMutableArray *names = nil;
//Compare the first character using diacritic insensitive search
if([str compare:firstChar options:NSDiacriticInsensitiveSearch|NSCaseInsensitiveSearch range:NSMakeRange(0, 1)] == NSOrderedSame)
{
names = [sectioned objectForKey:firstChar];
}
else
{
//decomposedStringWithCanonicalMapping is where the magic happens
//(it removes the accent mark)
firstChar = [[str decomposedStringWithCanonicalMapping] substringToIndex:1];
names = [NSMutableArray array];
[sectioned setObject:names forKey:firstChar];
}
[names addObject:str];
}
NSLog(@"sorted: %@", sorted);
//This is sectioned like the address app
NSLog(@"sectioned: %@", sectioned);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With