我如何排序在它NSNumbers一个NSMutable阵列? [英] How Do I sort an NSMutable Array with NSNumbers in it?
问题描述
我试图做一个高评分表,并在目标C(实际上,在总体目标C是我的挑战)阵列吸,所以我无法弄清楚如何排序。我试图做这样的事情(speudo code,我写这篇文章的动作风格,因为我更舒服):
I'm trying to make a high score table, and suck at arrays in objective c (actually, in general objective c is challenging for me), so I can't figure out how to sort. I'm trying to do something like this (speudocode, I'm writing this in actionscript style because I'm more comfortable with it):
highscores.addObjecttoArray(score)
highscores.sort(ascending)
但我不出来...我已经看到它的其他线程,但其使用的plist文件和材料,我不知道够不够目标C向他们学习。
But I can't figure it out... I've seen other threads about it, but their use plist files and stuff and I don't know enough objective c to learn from them.
推荐答案
您想做到这一点的一小段路?
Would you like to do that the short way?
如果您有的NSNumber
实例的可变数组:
If you have a mutable array of NSNumber
instances:
NSSortDescriptor *highestToLowest = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO];
[mutableArrayOfNumbers sortUsingDescriptors:[NSArray arrayWithObject:highestToLowest]];
尼斯和容易:)
您还可以执行与不变阵列描述相似排序,但你最终将一份副本,而不是就地排序。
You can also perform similar sorting with descriptors on immutable arrays, but you will end up with a copy, instead of in-place sorting.
这篇关于我如何排序在它NSNumbers一个NSMutable阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!