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

  • SEARCH
  • Home
  • 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 3757034
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:56:38+00:00 2026-05-19T09:56:38+00:00

I am trying to implement a binary search using objective-c blocks. I am using

  • 0

I am trying to implement a binary search using objective-c blocks. I am using the function indexOfObject:inSortedRange:options:usingComparator:. Here is an example.

// A pile of data.
NSUInteger amount = 900000;
// A number to search for.
NSNumber* number = [NSNumber numberWithInt:724242];

// Create some array.
NSMutableArray* array = [NSMutableArray arrayWithCapacity:amount];
for (NSUInteger i = 0; i < amount; ++i) {;
    [array addObject:[NSNumber numberWithUnsignedInteger:i]];
}
NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];

// Run binary search.
int index1 = [array indexOfObject:number 
                    inSortedRange:NSMakeRange(0, [array count]) 
                          options:NSBinarySearchingFirstEqual 
                  usingComparator:^(id lhs, id rhs) {
                      if ([lhs intValue] < [rhs intValue]) {
                          return (NSComparisonResult)NSOrderedAscending;
                      } else if([lhs intValue] > [rhs intValue]) {
                          return (NSComparisonResult)NSOrderedDescending;
                      }
                      return (NSComparisonResult)NSOrderedSame;
                  }]; 
NSTimeInterval stop1 = [NSDate timeIntervalSinceReferenceDate]; 
NSLog(@"Binary: Found index position: %d in %f seconds.", index1, stop1 - start);

// Run normal search.
int index2 = [array indexOfObject:number];
NSTimeInterval stop2 = [NSDate timeIntervalSinceReferenceDate];
NSLog(@"Normal: Found index position: %d in %f seconds.", index2, stop2 - start);   

I wonder how I can use an externally defined objective-c block with the aforementioned function. Here are two compare functions.

NSComparisonResult compareNSNumber(id lhs, id rhs) {
    return [lhs intValue] < [rhs intValue] ? NSOrderedAscending : [lhs intValue] > [rhs intValue] ? NSOrderedDescending : NSOrderedSame;
}
NSComparisonResult compareInt(int lhs, int rhs) {
    return lhs < rhs ? NSOrderedAscending : lhs > rhs ? NSOrderedDescending : NSOrderedSame;
}

Those are written in reference to the following declarations which can be found in NSObjCRuntime.h.

enum _NSComparisonResult {NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending};
typedef NSInteger NSComparisonResult;
typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);
  • 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-05-19T09:56:39+00:00Added an answer on May 19, 2026 at 9:56 am

    You can define a block as a global variable to get an effect similar to functions.

    NSComparisonResult (^globalBlock)(id,id) = ^(id lhs, id rhs) {
        if([lhs intValue] < [rhs intValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        } else if([lhs intValue] > [rhs intValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        return (NSComparisonResult)NSOrderedSame;
    };
    

    Then, in the method doing the comparison:

    int index1 = [array indexOfObject:number 
                        inSortedRange:NSMakeRange(0, [array count]) 
                              options:NSBinarySearchingFirstEqual 
                      usingComparator:globalBlock]; 
    

    To put the block in a header, for external use:

    NSComparisonResult (^globalBlock)(id,id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to implement a delete function for a Binary Search Tree but
I am trying to implement binary search tree operations and got stuck at deletion.
I was trying to implement binary search tree but I think I have made
trying to implement a dialog-box style behaviour using a separate div section with all
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I am trying to connect the siblings of the binary search tree. You can
I'm a Python guy. Learning C language and I've been trying to implement Binary
Im trying to implement fade in effect to my mp3 player. I´m using FloatControl
So, im trying to implement the bottomupheap algorithm here: http://www.apl.jhu.edu/Classes/Notes/Felikson/courses/605202/lectures/L8/L8.html Algorithm bottomUpHeap(S) Input: a
I am trying to implement reset password functionality using the tutorial at http://www.binarylogic.com/2008/11/16/tutorial-reset-passwords-with-authlogic/ and

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.