【发布时间】:2013-05-23 23:55:03
【问题描述】:
我有一个 NSMutableArray,包含 4 个对象。这些对象中的每一个都有一个称为 score 的 int 类型的属性。该数组按此属性降序排序。这么多就好了。
现在我正在制作一个记分牌,它接收这个数组并枚举它。基本上我需要它来查看 array.score 属性并为其分配排名。
第 1 - (100 分) 第 2 - (90 分) 第三 - (50 分) 第 4 - (10 分)
但问题是,当我的得分出现平局时,我需要它像这样出现:
第 1 - (100 分) 第一名 - (100 分) 第三 - (60 分) 第三 - (60 分)
基本上,我需要平局分数才能实际显示为平局。在这里的逻辑有点麻烦,似乎很简单......但我的大脑现在正在试图弄清楚这一点。
这是我所拥有的(这不起作用):
// If we have the same score, the rank does not change
int rank = 1;
int rankWithTies = 1;
int previousScore = 0;
NSLog(@"------- PLAYER SCORES ------");
for (PlayerStat *stat in rotatorSorted) {
if (stat.score < previousScore) {
NSLog(@"A %i. Player %i - score:%i", rank, stat.playerNumber, stat.score);
rankWithTies++;
} else {
previousScore = stat.score;
NSLog(@"B %i. Player %i - score:%i", rankWithTies, stat.playerNumber, stat.score);
}
rank++;
}
【问题讨论】: