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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T07:00:11+00:00 2026-05-17T07:00:11+00:00

I made a custom class. This is the h file @interface Player : NSObject

  • 0

I made a custom class. This is the h file

@interface Player : NSObject {
    NSString *name;
    NSNumber *points;
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSNumber *points;

-(id) initWithName:(NSString *)n andPoints:(int)p;

@end

This is the m file

#import "Player.h"

@implementation Player
@synthesize name, points;

-(id) initWithName:(NSString *)n andPoints:(int)p
{
    self.name = n;
    self.points = [NSNumber numberWithInt:p];
    return self;
}
@end

Then I create several players, and assign them to one of two teams like this:

Player *p1 = [[Player alloc] initWithName:@"Joe" andPoints:5];
Player *p2 = [[Player alloc] initWithName:@"James" andPoints:5];
Player *p3 = [[Player alloc] initWithName:@"Jim" andPoints:5];

NSMutableArray *team1 = [[NSMutableArray alloc] initWithObjects:p1,p2,p3,nil];

Player *p4 = [[Player alloc] initWithName:@"Aaron" andPoints:7];
Player *p5 = [[Player alloc] initWithName:@"Anthony" andPoints:7];
Player *p6 = [[Player alloc] initWithName:@"Alex" andPoints:7];

NSMutableArray *team2 = [[NSMutableArray alloc] initWithObjects:p4,p5,p6,nil];

Then I put these two teams in another NSMutableArray like this:

NSMutableArray *allTeams = [[NSMutableArray alloc] initWithObjects:team1, team2, nil];

To display all the players and their points, I use this loop:

for (NSMutableArray *teamArray in allTeams) {
    for (Player *player in teamArray) {
        NSLog(@"%@: %@", [player name], [player points]);
    }
}

As it is, it will just show the players in the order they are added above.

I want to sort this array of teams by the team’s points, which is just the sum of the points of each individual player in the team. Then when the loop runs to display all the players, the players in team2 will be displayed before team1.

What’s the code to sort the allTeams array by team points? Do I need to use categories?

  • 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-17T07:00:11+00:00Added an answer on May 17, 2026 at 7:00 am

    It’s actually going to be very simple. Do:

    [allTeams sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"@sum.points" ascending:NO]]];
    

    Hopefully that came out right. Typing code on an iPhone is a pain…

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

Sidebar

Related Questions

I made a custom logging class that logs certain things to a file. I'm
I've made an custom UILabel class in which i draw a red line at
I have a custom-made view that extends the View class. I would like 2
I made a custom master page. I also made a custom CSS file, which
I made a custom UITableView subclass and implemented this: - (void)scrollViewDidScroll:(UIScrollView *)scrollView { //
I made a custom ant script to automatically create a jar file each time
I have a custom made class called Graph in which I use adjacency lists.
I've made a custom MKAnnotation class, MapLocation. I'm having no trouble setting or getting
I have made a custom adapter class here is the code public class CustomArrayAdapterForReceipts
I have a custom view I made with an IB file since it's quite

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.