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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:13:34+00:00 2026-06-12T15:13:34+00:00

I’m have an NSArray that I need to sort based on variables that are

  • 0

I’m have an NSArray that I need to sort based on variables that are stored inside an object (in this case my class name is SchedView which is a UIViewController). I was thinking NSArray would let me pass it a “member function” (not sure if that is the term used in Objective-C or not, I’m new to this language). It appears I can only pass a static/C style function though, so this is what I ended up with:

- (NSInteger) BuildGridSortCompare :(id) r1 :(id) r2 :(void*) context
{
    // Have compare code here, and can use class scope variables...

    return NSOrderedSame;
}

NSInteger _BuildGridSortCompare(id r1,id r2,void *context)
{
    SchedView *sv = (__bridge SchedView*) context;
    return [sv BuildGridSortCompare:r1 :r2 :NULL];
}

- (void)BuildGrid
{
    Records = [Records sortedArrayUsingFunction:_BuildGridSortCompare context:(__bridge void *)(self)];
}

BuildGrid and BuildGridSortCompare are member functions of my SchedView class (selectors?). _BuildGridSortCompare is basically a C style function that is just a wrapper to call my member function.

I’ve used this design before in C/C++ code, but not in modern platforms such as Java and C#, so thought maybe Objective-C might have a better way?

I’m not real sure about some of the typecasts as well, the __bridge mostly.

Is what I am doing safe, or is there another function similar to sortedArrayUsingFunction that lets me use a member function? I did see sortedArrayUsingSelector, but that appears to call a function on the class I am sorting. In this case I want the function to be implemented by self.

  • 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-12T15:13:36+00:00Added an answer on June 12, 2026 at 3:13 pm

    I think that your code is OK, but you can do this much more elegantly using blocks:

    records = [records sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
        // compare code here, can use self etc.
        return ...;
    }];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this

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.