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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:18:21+00:00 2026-06-09T00:18:21+00:00

When I sort myArray using [myArray sortArrayUsingSelector: @selector(comparison:)]; fracC doesnt get compared with fracA

  • 0

When I sort myArray using [myArray sortArrayUsingSelector: @selector(comparison:)]; fracC doesnt get compared with fracA, so now fracA (1/2) is placed behind of fracC(1/4)
in order of greatest value to lowest. Is there a way to get all the elements compared to each other correctly and for it to still work if I wanted to add more values in?

comparison:

-(NSComparisonResult) comparison:(Fraction *)myFrac {
    NSLog(@"self: %@", self);
    NSLog(@"argument: %@", myFrac);

    if ([self compare: myFrac] == 1) {
        NSLog(@"ascending");
        NSLog(@" ");
        return NSOrderedAscending;
    }
    else if ([self compare: myFrac] == -1) {
        NSLog(@"descending");
        NSLog(@" ");
        return NSOrderedDescending;
    }
    else {
        NSLog(@"same");
        NSLog(@" ");
        return NSOrderedSame;
    }
}

comparison: output

2012-08-03 16:25:43.459 prog[1083:707] self: 1/3
2012-08-03 16:25:43.462 prog[1083:707] argument: 1/2
2012-08-03 16:25:43.463 prog[1083:707] descending
2012-08-03 16:25:43.463 prog[1083:707]  
2012-08-03 16:25:43.464 prog[1083:707] self: 1/3
2012-08-03 16:25:43.464 prog[1083:707] argument: 1/4
2012-08-03 16:25:43.465 prog[1083:707] ascending

main.m

        Fraction *fracA = [[Fraction alloc] initWithValues:1 over:2];
        Fraction *fracB = [[Fraction alloc] initWithValues:1 over:3];
        Fraction *fracC = [[Fraction alloc] initWithValues:1 over:4];

        NSMutableArray *myArray = [NSMutableArray arrayWithObjects:fracB, fracA, fracC, nil];

        [myArray sortArrayUsingSelector: @selector(comparison:)];

        for (Fraction *myFrac in myArray) {
            NSLog(@"%@", myFrac);
        }

output of main.m

2012-08-03 16:25:43.467 prog[1083:707] 1/3
2012-08-03 16:25:43.467 prog[1083:707] 1/2
2012-08-03 16:25:43.468 prog[1083:707] 1/4

compare:

-(int) compare:(Fraction *)f {
    Fraction *selfTester = [[Fraction alloc] init];
    Fraction *fTester = [[Fraction alloc] init];

    selfTester.denominator = self.denominator * f.denominator;
    selfTester.numerator = f.denominator * self.numerator;

    fTester.denominator = self.denominator * f.denominator;
    fTester.numerator = f.numerator * self.denominator;


    if (selfTester.numerator < fTester.numerator)
        return -1;
    else if (selfTester.numerator == fTester.numerator)
        return 0;
    else
        return 1;
}
  • 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-09T00:18:23+00:00Added an answer on June 9, 2026 at 12:18 am

    sortedArrayUsingSelector: returns a new sorted array (which you discard).

    Your probably want to use sortUsingSelector: which sorts the elements of the array in place.

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

Sidebar

Related Questions

I can sort a int* array using stl, plain and simple like std::sort(myarray, myarray
...@Sort bit) AS SELECT .............. and I want to Order only if Sort =
Comparison sort is opted for in most of the scenarios where data needs to
I'm trying to pre-sort an array of items are added into an array using
Is there a way to sort an array using Chrome? Using the sort function
Hi I am using the following insertion sort algorithm and I would like to
I'm try to order objects numerically in an array in ascending order, by using
If have an array of Javascript IDs var myArray = ['2', '1', '3']; Now,
I am trying to sort my array while using NSPredicate.. I have read other
I have been using UIPickerController to get the pics from my Built-in-Gallery into my

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.