All,
I am trying to use predicates to bring back a search return, giving precedence to strings that start with the search string VS. simply contained within it.
For example if the search string was “Objective-C”, I want to get the filtered results back like this:
Objective-C a Primer
Objective-C Patterns
Objective-C Programming
All About Objective-C
How to program in Objective-C
Here is what I tried but since it’s an OR, it clearly does not give precedence to the first condition. Is there a way to do a type of “chaining” with predicates? Thanks
NSPredicate *filter = [NSPredicate predicateWithFormat:@"subject BEGINSWITH [cd] %@ OR subject CONTAINS [cd]", searchText,searchText];
NSArray *filtered = [myArray filteredArrayUsingPredicate: filter];
I don’t think there’s a way to do with NSPredicate by itself. What you seem to be asking for is sorted results. You do that by sorting the results after you get them back from the predicate. In this case, since the sort order isn’t simply alphabetical, you should use NSArray’s
-sortedArrayUsingComparator:method. Something like this (not tested, typed off the top of my head).