Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7753221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:00:04+00:00 2026-06-01T12:00:04+00:00

All, I am trying to use predicates to bring back a search return, giving

  • 0

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];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:00:06+00:00Added an answer on June 1, 2026 at 12:00 pm

    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).

    NSArray *sortedStrings = [filtered sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        NSString *string1 = (NSString *)obj1;
        NSString *string2 = (NSString *)obj2;
        NSUInteger searchStringLocation1 = [string1 rangeOfString:searchString].location;
        NSUInteger searchStringLocation2 = [string2 rangeOfString:searchString].location;
        if (searchStringLocation1 < searchStringLocation2) return NSOrderedDescending;
        if (searchStringLocation1 > searchStringLocation2) return NSOrderedAscending;
        return NSOrderedSame;
    }];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use Linq2Sql to return all rows that contain values from
I'm trying to use XPath to find all elements that have an element in
I'm trying to use an NSPredicate to search for string matches against an array
Hey all i am trying to use my already array that i populated into
I'm trying use preg_match in an IF statement and return false if a string
I'm trying to use VDMEnumProcessWOW to find all 16 bit host processes on Vista.
I'm trying to use JQuery to select all of the TD 's in a
I'm trying to use the PowerShell Add-Member cmd on all the items in an
I'm trying to use the getElementsByTagName(a) method to get all the elements under a
I'm trying to use Linq to loop through all fonts in the %windir%\Fonts folder

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.