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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:26:18+00:00 2026-05-14T14:26:18+00:00

I have a class (from NSObject ) that contains: NSString name int position float

  • 0

I have a class (from NSObject) that contains:

NSString name
int      position
float    speed

I then create an array (NSMutableArray) of objects from this class. I would like to then sort the array by the ‘speed’ value, which is a float.

I initially has the float value as an NSNumber and the int as NSInteger, and I was successfully sorting with:

[myMutableArray sortUsingFunction:compareSelector context:@selector(position)];

where myMutableArray is my array of objects.

here is the function:

static int compareSelector(id p1, id p2, void *context) {
    SEL methodSelector = (SEL)context;
    id value1 = [p1 performSelector:methodSelector];
    id value2 = [p2 performSelector:methodSelector];
    return [value1 compare:value2];
}

Now that I am using int instead of NSInteger, the above code does not work. Is there a more low-level command that I should be using to execute the sort? Thank!

  • 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-14T14:26:19+00:00Added an answer on May 14, 2026 at 2:26 pm

    Similar to drawnonward, I’d suggest adding a comparison method to your class:

    - (NSComparisonResult)compareSpeed:(id)otherObject
    {
        if ([self speed] > [otherObject speed]) {
            return NSOrderedDescending;
        } else if ([self speed] < [otherObject speed]) {
            return NSOrderedAscending;
        } else {
            return NSOrderedSame;
        }
    }
    

    (You could collapse the if–else if–else using the ternary operator: test ? trueValue : falseValue, or if speed is an object with a compare: method (such as NSNumber), you could just return [[self speed] compare:[otherObject speed]];.)

    You can then sort by

    [myMutableArray sortUsingSelector:@selector(compareSpeed:)];
    

    As suggested by Georg in a comment, you can also achieve your goal using NSSortDescriptor; if you’re targeting 10.6, you can also use blocks and sortUsingComparator:(NSComparator)cmptr.

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

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a class that inherits from UIView However when trying to use the
I have a UIScrollView that contains multiple UITableViews horizontally - all of which are
I have a class which derived from the TableLayoutPanel. This class makeup the tabel
I have a class which inherits from the System.Web.UI.Page class, like mysystem . When
I have a generic abstract base class from which I would like to derive
I have two view controllers A and B. From A, I navigate to view
I have a python class and within the class I call 2 different methods
I have a class Wizard which creates a wizard with pages defined in the
Long term lurker, first time poster here. I have written a class to model
My app is consisted of 2 views. The MainView and ResultsView. MainView contains a

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.