2
晚上好。如何对NSMutableArray进行排序?代码审查需要
此代码有效。它根据西装和卡片价值排列一系列卡片。这也是非常强大的力量。你能推荐一个更好的方法吗? Objective-C是否帮助处理对象被排序的情况有多个字段,排序依赖于哪个字段?
-(void) sort: (NSMutableArray *) deck {
NSUInteger count = [deck count];
Card *thisCard;
Card *nextCard;
int this;
int next;
BOOL stillSwapping = true;
while (stillSwapping) {
stillSwapping = false;
for (NSUInteger i = 0; i < count; ++i) {
this = i;
next = i+1;
if (next < count) {
thisCard = [deck objectAtIndex:this];
nextCard = [deck objectAtIndex:next];
if ([thisCard suit] > [nextCard suit]) {
[deck exchangeObjectAtIndex:this withObjectAtIndex:next];
stillSwapping = true;
}
if ([thisCard suit] == [nextCard suit]) {
if ([thisCard value] > [nextCard value]) {
[deck exchangeObjectAtIndex:this withObjectAtIndex:next];
stillSwapping = true;
}
}
}
}
}
}
+ 1 - 漂亮的代码示例 – bryanmac
很不错确实如此。谢谢 – JAM
优秀的答案。我建议在你的类上使用实现'compare:'的建议(使用块中的代码进行比较)。 – bbum