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 8794823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:18:13+00:00 2026-06-13T23:18:13+00:00

I am trying to wrap my head around block programming, and currently stuck in

  • 0

I am trying to wrap my head around block programming, and currently stuck in this problem

char *myCharacters[3] = { "TomJohn", "George", "Charles Condomine" };
qsort_b(myCharacters, 3, sizeof(char *), ^(const void *l, const void *r) {
    char *left = *(char **)l;
    char *right = *(char **)r;
    return strncmp(left, right, 1); 
});

On second line the block parameter ^(const void *l, const void *r), from where it is getting its parameter values.

From the Apple Block Programming Topics documentation:

Blocks with Cocoa

Several methods in the Cocoa frameworks take a block
as an argument, typically either to perform an operation on a
collection of objects, or to use as a callback after an operation has
finished. The following example shows how to use a block with the
NSArray method sortedArrayUsingComparator:. The method takes a single
argument—the block. For illustration, in this case the block is
defined as an NSComparator local variable:

What is the meaning of “in this case the block is defined as an NSComparator local variable”?

This is the code sample provided

NSArray *stringsArray = @[ @"string 1",
                         @"String 21",
                         @"string 12",
                         @"String 11",
                         @"String 02" ];
static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch |
                                                  NSNumericSearch |
                                                  NSWidthInsensitiveSearch |
                                                  NSForcedOrderingSearch;
NSLocale *currentLocale = [NSLocale currentLocale];
NSComparator finderSortBlock = ^(id string1, id string2) {
    NSRange string1Range = NSMakeRange(0, [string1 length]);
    return [string1 compare:string2 
                    options:comparisonOptions 
                      range:string1Range
                     locale:currentLocale];
};
NSArray *finderSortArray = [stringsArray sortedArrayUsingComparator:finderSortBlock];     
NSLog(@"finderSortArray: %@", finderSortArray);

Where is ^(id string1, id string2) getting its parameter values?

  • 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-13T23:18:14+00:00Added an answer on June 13, 2026 at 11:18 pm

    NSComparator is actually a block typecasted as follows, typedef NSComparisonResult (^NSComparator)(id obj1, id obj2); same way as typedef double NSTimeInterval; or typedef long NSInteger;. Since it is a block, the format is slightly different with arguments. Basically NSComparator is a block which accepts two params obj1 and obj2 and returns an NSComparisonResult value to denote the ordering of the two objects. It can return NSOrderedAscending, NSOrderedSame or NSOrderedDescending. This can be used by sortedArrayUsingComparator to repeatedly compare two objects in array and sort it based on that. This also helps in implementing our own implementation for sorting. When the sorting happens this block is called by sortedArrayUsingComparator and gives values to obj1 and obj2 and executes the NSComparator block. From that block it returns NSComparisonResult based on the comparison we implemented.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to wrap my head around functional programming concepts. Consider the problem of
I'm trying to wrap my head around this and I think the easiest way
I'm trying to wrap my head around Object Oriented programming. But I'm having some
I'm trying to wrap my head around this new concept called LINQ. Ever heard
Trying to wrap my head around this function any_name($div,$width = false){ if($width){ $add_style ='
I'm trying to wrap my head around this to make the correct design decisions.
I'm trying to wrap my head around this, but I seem to go in
I am trying to wrap my head around object oriented programming. My understanding is
I'm trying to wrap my head around the basic concepts of the R programming
Ok, I've been trying to wrap my head around this for some time now

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.