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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:39:49+00:00 2026-05-26T23:39:49+00:00

Good evening. This code works. It sorts an array of cards by both Suit

  • 0

Good evening.

This code works. It sorts an array of cards by both Suit and Card Value. It is also very much brute force. Can you recommend a better way? Does Objective-C help dealing with a situation where object being sorted itself has multiple fields, on which sorting depends?

-(void) sort: (NSMutableArray *) deck {        
    NSUInteger count = [deck count];

    Card *thisCard;
    Card *nextCard;

    int this;
    int next;

    BOOL stillSwapping = true;

    while (stillSwapping) {

        stillSwapping = false;

        for (NSUInteger i = 0; i < count; ++i) {
            this = i;
            next = i+1;

            if (next < count) {

                thisCard = [deck objectAtIndex:this];
                nextCard = [deck objectAtIndex:next];

                if ([thisCard suit] > [nextCard suit]) {                        
                    [deck exchangeObjectAtIndex:this withObjectAtIndex:next];
                    stillSwapping = true;
                } 


                if ([thisCard suit] == [nextCard suit]) {
                    if ([thisCard value] > [nextCard value]) {
                        [deck exchangeObjectAtIndex:this withObjectAtIndex:next];
                        stillSwapping = true;
                    } 
                }
            }
        }
    }
}
  • 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-26T23:39:50+00:00Added an answer on May 26, 2026 at 11:39 pm

    You have several options.

    Perhaps the simplest is to define the method -compare: on Card, which returns an NSComparisonResult. This is preferable if sorting by suit and value is the “standard” way to sort a Card. If you’ve done this, then your entire sorting method can be turned into

    [deck sortUsingSelector:@selector(compare:)];
    

    If you don’t want to do this, you can use an array of NSSortDescriptors, one for suit and one for value.

    [deck sortUsingDescriptors:[NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"suit" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@"value"] ascending:YES], nil]];
    

    If this doesn’t work for whatever reason, you could try -sortUsingComparator:

    [deck sortUsingComparator:^NSComparisonResult(Card *obj1, Card *obj2) {
        int suit1 = [obj1 suit];
        int suit2 = [obj2 suit];
        if (suit1 > suit2) {
            return NSOrderedDescending;
        } else if (suit1 < suit2) {
            return NSOrderedAscending;
        } else {
            id value1 = [obj1 value];
            id value2 = [obj2 value];
            if (value1 > value2) {
                return NSOrderedDescending;
            } else if (value1 < value2) {
                return NSOrderedAscending;
            } else {
                return NSOrderedSame;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good evening :-)! I have this code to use Drag & Drop method for
Good Evening, I'm using this script, it works really good but I want it
Good Evening folks. This is my code: static private function removeAccentedLetters($input){ for ($i =
Good morning, afternoon, evening or night (depending on your timezone). This is just a
Good evening. For a project, I have to create a system. In this system,
Good evening fellow coders, and thank you in advance for any help/attention. This is
Good evening, I am tired and have been fighting with this for hours. I
Good Evening, I'm building a website which will will look something like this: So
Good evening! I'm developing a web application with asp.net C#. In this application the
Good evening, Following is the code I used for reading the files and folders

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.