Reputation: 3926
I have an array that contains some core data objects. Each object has three attributes say "First Name
" & "Last Name
"
I want to filter & sort the array as per the search letter(s). The final list should be in this order:
I confused in creating predicates and sort descriptors. Can anyone help?
Edit:
Here is my fetched results controller:
-(NSFetchedResultsController *)myFetchedResultsController
{
if (!_myFetchedResultsController)
{
...
...
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:MY_ENTITY_NAME];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(firstName beginswith[cd] %@) || (lastName beginswith[cd] %@) || (firstName contains[cd] %@) || (lastName contains[cd] %@))", self.searchText, self.searchText, self.searchText, self.searchText];
[fetchRequest setPredicate:predicate];
//I really dont have idea about how to set sort description to this scenario
NSSortDescriptor *firstNameDescriptors = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSSortDescriptor *lastNameDescriptors = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
[fetchRequest setSortDescriptors:@[firstNameDescriptors, lastNameDescriptors]];
_userFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
}
return _myFetchedResultsController;
}
But this seems not working, means it does not sort the objects.
Upvotes: 0
Views: 749
Reputation: 53010
But this seems not working, means it does not sort the objects.
Do you really mean it does not sort, or that it does not sort they way you wish?
NSSortDescriptor
takes a single key on which to sort. In a situation where you have a primary key and a secondary key where you sort using the secondary key only if the primary keys are equal, e.g. family name then given name, then routines such as the setSortDescriptors:
exist which take an array of sort descriptors. This is the kind of sort you have setup in your code.
However this is not the kind of sort you have specified in your bullets. Your sort requires access to multiple keys to determine the order - it says of the first key meets a particular condition then sort on that key, else if the second key meets a particular condition then sort on that key, etc. To perform this kind of sort you can write an NSComparator
block which has access to the complete objects - not just the values of a particular key - and implement your algorithm as a combination of tests and comparisons.
Unfortunately for you this second kind of sort is not supported directly by NSFetchRequest
. Instead you will need to obtain the results without sorting and then sort the resultant array with a method such as sortedArrayUsingComparator:
.
For a related question see NSSortDescriptor: Custom comparison on multiple keys simultaneously.
HTH
Upvotes: 1