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.
You could split up the results into two arrays, one that starts with alpha characters, and one that doesn't. Then just add the two together. Assuming you are starting with an array of managed objects called
results
:You could try writing your own comparator Function
Assuming that it is sorting ManagedObjects and they all have fullName as a field, then the following may help.
The advantage of this is you can also write the NSLog for each comparison and see what is happening.
You could store an additional attribute
sortName
in the entity, which isfullName
if the name starts with a letter, and<C>fullName
otherwise.<C>
is a fixed character which is "greater" than all letters. For exampleNow you can sort according to
sortName
, and the section identifier would be "#" ifsortName
starts with the special character.The disadvantage is that you have to store an additional attribute, the advantage is that you can continue to use a fetched results controller (which can use only persistent attributes for sorting).
UPDATE: It can actually be done a bit easier.
When you create a new entry, you set
sectionIdentifier
to the first character of the name if it is a letter, and to the special character otherwise:The fetched results controller uses
sectionIdentifier
for grouping and sorting the sections. Entries within each section are sorted bycontactName
:All non-letter entries are now grouped in the last section. The final step is to display the correct section header
#
for the last section:You can do this:
Just make sure it doesn't affect performance in your case.