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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:44:31+00:00 2026-05-11T19:44:31+00:00

For a little iPhone application I am making, I want to sort a NSMutableArray.

  • 0

For a little iPhone application I am making, I want to sort a NSMutableArray.

I found 2 ways of doing this, but they both result in the same thing. Sorting the array will cause some objects to ‘overwrite’ eachother.

First off, here is my code:

AppDelegate.h

NSMutableArray* highScores;

Somewhere down that AppDelegate.h, I also make this variable a property so that I can access it from differen classes:

@property (retain, nonatomic) NSMutableArray* highScores;

When my application starts, I read the high scores from a file and import them into my NSMutableArray.

AppDelegate.m

NSMutableData* data = [NSData dataWithContentsOfFile:highScoresPath];
NSKeyedUnarchiver* decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
self.highScores = [decoder decodeObjectForKey:@"highscoresArray"];

The objects I store in this NSMutableArray are from the type HighScore.

HighScore.h

@interface HighScore : NSObject {

    int score;
    int roundsPlayed;
    int wrongAnswers;

    NSString* name;

    NSDate* datetime;
}

@property int score;
@property int roundsPlayed;
@property int wrongAnswers;
@property (nonatomic, copy) NSDate* datetime;

@property (nonatomic, copy) NSString* name;

- (id) init;
- (void) update:(int)roundScore:(BOOL) correct;

@end

HighScore.m

#import "HighScore.h"

@implementation HighScore

@synthesize score, roundsPlayed, wrongAnswers, name, datetime;

- (id) init
{
    self.name = @"";
    self.score = 0;
    self.roundsPlayed = 0;
    self.wrongAnswers = 0;

    self.datetime = [NSDate date];

    return self;
}

- (void) update:(int)roundScore:(BOOL) correct
{
    self.score += roundScore;

    if (!correct)
        self.wrongAnswers++;

    self.roundsPlayed++;
    self.datetime = [NSDate date];
}

- (id) initWithCoder:(NSCoder *) decoder 
{   
    self.name = [[decoder decodeObjectForKey:@"name"] retain];
    self.score = [decoder decodeIntForKey:@"score"];
    self.roundsPlayed = [decoder decodeIntForKey:@"roundsPlayed"];
    self.wrongAnswers = [decoder decodeIntForKey:@"wrongAnswers"];
    self.datetime = [[decoder decodeObjectForKey:@"datetime"] retain];

    return self;
}

- (void) encodeWithCoder:(NSCoder *)encoder 
{
    [encoder encodeObject:self.name forKey:@"name"];
    [encoder encodeInt:self.score forKey:@"score"];
    [encoder encodeInt:self.roundsPlayed forKey:@"roundsPlayed"];
    [encoder encodeInt:self.wrongAnswers forKey:@"wrongAnswers"];
    [encoder encodeObject:self.datetime forKey:@"datetime"];
}

- (NSComparisonResult) compareHighscore:(HighScore*) h
{

    return [[NSNumber numberWithInt:self.score] compare:[NSNumber numberWithInt:h.score]];
}

@end

Now, when I try to sort my array by using the following code:

NSArray *sortedArray;
sortedArray = [highScores sortedArrayUsingSelector:@selector(compareHighscore:)];

It somehow screws up my highScores array, I get an X amound of highscores with the same score and name.

What am I doing wrong?

  • 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-11T19:44:32+00:00Added an answer on May 11, 2026 at 7:44 pm

    I’m noticing that in your initWithCoder: method, you’re not doing this:

    if (self = [super initWithCoder:coder]) {
        // Decode your stuff here
    }
    

    Same with your regular init method. There needs to be a call to [super init].

    Also, since you defined your string properties as copy and you’re using the property syntax, there’s no need to retain them. They will be retained for you by the synthesized accessor.

    Otherwise, your code looks fine to me. Just remember: every init method must always have a call to a super‘s init... method.

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

Sidebar

Ask A Question

Stats

  • Questions 451k
  • Answers 451k
  • 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 The enum will have to be made public to be… May 15, 2026 at 8:51 pm
  • Editorial Team
    Editorial Team added an answer You will find more than you ever wanted to know… May 15, 2026 at 8:51 pm
  • Editorial Team
    Editorial Team added an answer There is no defined syntax to use array initializer syntax,… May 15, 2026 at 8:51 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

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.