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:
- Records that starts with first name as like searched text should come first
- Records that starts with last name as like searched text should come second
- Records that contains first name as like searched text should come third
- Records that contains last name as like searched text should come fourth
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.