I am making a contact book App where I am fetching names from AddressBook and stored them in Core data and displayed the names on a table using NSFetchedResultsController.However
the first index and section that comes up is # followed by the alphabets. But I want to do it like it is in native contact app i.e. # index should come at last.
I used the following NSortDescriptor
:
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"fullName" ascending:YES ];
here "fullName" is the key
in core data which is made by concatenating first name and last name.
And the section identifier is the first letter of "fullName" if the fullName
doesn't start with alphabet, its section identifier is #.
I had searched about it and used NSDiacriticInsensitiveSearch
in the NSortDescriptor
comparator but it didn't worked. If any one has any idea then let me know.
Here goes my code:
NSString *special = @"\uE000";
if ([[self sectionName:contactName] isEqualToString:@"#"]) {
sortName = [special stringByAppendingString:contactName];
}
else{
sortName = contactName;
}
[newContact setValue:[self sectionIdentifier:sortName] forKey:@"sectionIdentifier"];
[newContact setValue:sortName forKey:@"sortName"];
And here is the sort descriptor:
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sortName" ascending:YES];
[self sectionIdentifier:sortName]
this method returns # if sortName starts with a non alphabet and else it returns the alphabet by which it starts.
newContact is the object of the entity.