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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:14:48+00:00 2026-05-15T13:14:48+00:00

I have been going over this for hours and cannot find the problem. I

  • 0

I have been going over this for hours and cannot find the problem.
I have an array of scores which I save at the end of a game in a core data model. After saving when I go back into the high scores viewcontroller I load the scores from core data and sort the array using the following function where scores array is an NSMutableArray with my 10 high scores

[self.scoresArray sortUsingFunction:firstNumSort context:@selector(totalScore)];

//function to sort high scores in the array
NSInteger firstNumSort(id str1, id str2, void *context) {
    static int counter = 0;

    NSLog(@"counter = %d", counter);
    counter++;
    //NSLog(@"should be sorting");
    int num1 = [str1 totalScore];
    int num2 = [str2 totalScore];
    if (num1 > num2) {
        NSLog(@"%@ is greater than %@", num1, num2);
        return NSOrderedAscending;
    }
    else if (num1 < num2){
        NSLog(@"%@ is less than %@", num1, num2);
        return NSOrderedDescending;
    }
else {
    NSLog(@"%@ is the same as %@", num1, num2);
    return NSOrderedSame;
    }

}

This always places the last score entry at the top of the list regardless of its value?? The stranger thing is that when I restart my app the same function puts all scores in correct descending order which is what I want, but obviously I dont want the user to have to restart the app to see the scores displayed in the correct order. Can anyone please shed some light on what is going on??

Many thanks

Jules

  • 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-15T13:14:49+00:00Added an answer on May 15, 2026 at 1:14 pm

    The only obvious problem I see with your code is that the context parameter you are passing to sortUsingFunction:context (i.e. the @selector(totalScore)) is not used by your function and could be replaced with NULL. That wouldn’t account for the behavior you are seeing though and is more a matter of style anyway. One other thing you might want to check is that the type of totalScore in really an int. Your NSLog statements in the sort function are treating the as if they are objects (the %@ is the format specifier for an object, for and int you would use %d.) Are you sure that total score is not actually NSString or an NSNumber? Other than that the sort function looks like it should work, and since you say that it does on start up, I would look outside of this code for the problem.

    Have you verified, either in the debugger or via a NSLog statement that the array is actually mis-sorted after the call? Perhaps there is some bug in how you are displaying the sorted scores or something is happening after the sort to change the order?

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

Sidebar

Related Questions

I've switched over to a Mac recently and, although things have been going quite
I have been going back and forth between C# and Java for the last
I have been tasked with going through a number of ColdFusion sites that have
I have been using C# for a while now, and going back to C++
I have been experimenting with woopra.com A web analytics tool. Which requires a piece
Have been looking at the MVC storefront and see that IQueryable is returned from
Have been studying the file system related classes of Adobe AIR 1.5, but so
We have been using CruiseControl for quite a while with NUnit and NAnt. For
I have been working on a web services related project for about the last
I have been working with Visual Studio (WinForm and ASP.NET applications using mostly C#)

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.