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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:40:40+00:00 2026-05-24T16:40:40+00:00

I have a sorting comparator which I need to use in a few different

  • 0

I have a sorting comparator which I need to use in a few different ViewControllers so I’m trying to keep it in a separate file. I’ve read I should be able to put the sorting block in a separate file, but which ever method I try I seem to get “unrecognized selector sent to instance”. The code I have is:

#import <Foundation/Foundation.h>

typedef NSComparator (^IntBlock)(id obj1, id obj2);

@interface Utils : NSObject {
    NSComparator SortObjNameComparer;
    NSComparator SortObjPriceComparer;
}
@property (readwrite, copy) NSComparator SortObjNameComparer;
@property (readwrite, copy) NSComparator SortObjPriceComparer;

To sort this I’m using

Utils *comp = [[Utils alloc] init];
if(segmentedControl.selectedSegmentIndex == 0){
    self.productArray = [self.productArray sortedArrayUsingComparator:[comp SortObjNameComparer] context:nil];   //EXC_BAD_ACCESS     
} else if(segmentedControl.selectedSegmentIndex == 1){
    self.productArray = [self.productArray sortedArrayUsingComparator:[Utils SortObjPriceComparer]];// EXC_BAD_ACCESS

Is there a clean way to do this?

  • 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-24T16:40:40+00:00Added an answer on May 24, 2026 at 4:40 pm

    Another way to do this is to create a C function that returns a block. That way you can create this block wherever you like:

    e.g

    MyComparator.h

    #import <Foundation/Foundation.h>
    
    NSComparator CreateObjNameComparer(void);
    NSComparator CreateObjPriceComparer(void);
    

    MyComparator.m

    #import MyComparator.h
    
    NSComparator CreateObjNameComparer(void) {
        NSComparator myComparator = ^(id obj1, id obj2) {
            // Your definition here
        };
        return Block_copy(myComparator);
    }
    
    NSComparator CreateObjPriceComparer(void) {
        NSComparator myComparator = ^(id obj1, id obj2) {
            // Your definition here
        };
        return Block_copy(myComparator);
    }
    

    And then all you need to do it use these is to import the header and use it as:

    NSComparator nameComparator = CreateObjNameComparor();
    // Use the block
    Block_release(nameComparator);
    

    Saves having to go through the whole Object creation and property route.

    Note that I’m using the Create naming convention to remind me that I have to release the object myself when I’m done with it.

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

Sidebar

Related Questions

I have a list of objects and Comparator which is used for sorting and
I have a duplicate bridge scoring application with two different sorting modes. Let me
I'm sorting array: myArray.sort(comparators.some_comparator); and I have several comparator to choose from: comparators =
I want to have different sorting and filtering applied on my view I figured
There's a few algorithms we have for sorting data, finding the maximum and minimum,
I have a WPF sorting/binding issue. (Disclaimer: I am very new to WPF and
I have a complex sorting problem with my SQL statement. I have a table
I have a problem with sorting NSTableColumn contents. In my NSTableView there are three
I have a problem with sorting items in table in PHP. Here is what
I have a spreadsheet that I am sorting based on an item number column.

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.