简体   繁体   English

使用核心数据对NSMutableArray怪异结果进行排序

[英]sorting NSMutableArray weird results with Core Data

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 保存后,当我返回高分视图控制器时,我从核心数据加载了分值,并使用以下函数对数组进行了排序,其中scores array是我的10分高分的NSMutableArray

[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 朱尔斯

The only obvious problem I see with your code is that the context parameter you are passing to sortUsingFunction:context (ie the @selector(totalScore) ) is not used by your function and could be replaced with NULL . 我在您的代码中看到的唯一明显问题是,您传递给sortUsingFunction:context的上下文参数(即@selector(totalScore) )未被您的函数使用,可以替换为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 . 您可能要检查的另一件事是inttotalScore类型。 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 ? 您的NSLog语句在sort函数中将它们视为对象( %@是对象的格式说明符,对于int,您将使用%d 。)您确定总分实际上不是NSStringNSNumber 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. 除此之外,sort函数看起来应该可以工作,而且由于您说它在启动时就可以工作,因此我将在此代码之外查找该问题。

Have you verified, either in the debugger or via a NSLog statement that the array is actually mis-sorted after the call? 您是否在调试器中或通过NSLog语句验证了调用后该数组实际上是错误排序的? Perhaps there is some bug in how you are displaying the sorted scores or something is happening after the sort to change the order? 也许您在显示排序分数时存在一些错误,或者排序更改后发生了什么变化?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM