【发布时间】:2010-06-30 15:43:40
【问题描述】:
我已经研究了好几个小时,但找不到问题所在。 我有一组分数,我在游戏结束时将它们保存在核心数据模型中。保存后,当我回到高分视图控制器时,我从核心数据加载分数并使用以下函数对数组进行排序,其中分数数组是一个 NSMutableArray,有我的 10 个高分
[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;
}
}
无论它的值如何,这总是将最后一个分数条目放在列表的顶部??奇怪的是,当我重新启动我的应用程序时,相同的功能会将所有分数按正确的降序排列,这是我想要的,但显然我不希望用户必须重新启动应用程序才能看到以正确顺序显示的分数。任何人都可以阐明发生了什么吗??
非常感谢
朱尔斯
【问题讨论】:
标签: iphone cocoa-touch sorting core-data