I am searching a CoreData entity by an attribute with a BEGINSWITH predicate. Now I would like to sort the results by relevance, so that direct matches appear first.
Example search string:
"star"
Result list:
"star" # <- exact match should be first
"starship"
"stargazer"
I have a CoreData fetch request like this:
NSString *stringFromSearchField;
NSFetchRequest *fr =[NSFetchRequest fetchRequestWithEntityName:@"SearchWord"];
fr.predicate = [NSPredicate predicateWithFormat:@"(word beginswith %@)", stringFromSearchField];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Exact matches first, please..." ascending:YES];
fr.sortDescriptors = @[sortDescriptor];
I am aware that I could copy the results from my NSFetchedResultsController into a mutable array and sort in code, but I would like to avoid that if possible.
So is there any way I could archive this? Maybe some smart change in the data model or a predicate trick?