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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:13:12+00:00 2026-05-26T06:13:12+00:00

I have read Sort NSArray using sortedArrayUsingFunction and it’s possible that the following question

  • 0

I have read Sort NSArray using sortedArrayUsingFunction and it’s possible that the following question is about to reveal my deep ignorance of Objective-C.

The answers to the questions above suggest that I need a method whose signature is of the form
- (NSInteger) sortFunction(id id1, id id2, void *context);.

But all the code I have has function signatures of the form

- (NSInteger) sortFunction:(id) id1, foo:(id) id2, bar:(void *) context;

When I change my sortFunction’s declaration to be of the first form, I get all kinds of errors: A “expected ‘;’ before ‘(‘ token” in both myclass.h and myclass.m, and in a few other files; the same “‘sortFunction’ not declared” message that I get the other way; and “expected ‘{‘ before ‘(‘ token” on the declaration of sortFunction in myclass.m.

When I use it in the second form, I get an error on my call of sortedArrayUsingFunction — namely “function “sortFunction” not declared”.

What am I missing?

  • 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-26T06:13:12+00:00Added an answer on May 26, 2026 at 6:13 am

    This method requires an actual function, not a method. If you want to use a method, you would use sortedArrayUsingSelector: and it’s expected that this method will be defined on the objects themselves. So, if you have an array of NSStrings, for example, you’d call some compare selector on NSString. To use the function, you’d do something like this:

    // this orders by the object's memory location -- not really very useful in the real world
    NSInteger my_compare_function(id obj1, id obj2, void* context) {
      if( obj1 < obj2 ) return NSOrderedAscending;
      else if( obj1 > obj2 ) return NSOrderedDescending;
      else return NSOrderedSame;
    }
    

    This function needs to be somewhere in scope of where you call it, and then you’d call it like this:

    NSArray* sortedArray = [unsortedArray sortedArrayUsingFunction:my_compare_function context:NULL];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read that using database keys in a URL is a bad thing
I have read much about lambda,expression trees amd sort of compiled stuff... now where
I am trying to sort my array while using NSPredicate.. I have read other
I have read over this which sort of gives an explanation of when you'd
I have read a lot that LISP can redefine syntax on the fly, presumably
I have read about partial methods in the latest C# language specification , so
I have read this post about how to test private methods. I usually do
I have read on Stack Overflow some people that have converting to C#2.0 to
I have read (or perhaps heard from a colleague) that in .NET, TransactionScope can
I have read a counting sort algorithm which is like this: Counting Sort(A[1,..n]) //C[1,...k]

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.