如何对包含 NSNumbers 的 NSMutable 数组进行排序? [英] How Do I sort an NSMutable Array with NSNumbers in it?
问题描述
我正在尝试制作一个高分表,并在目标 c 中使用数组(实际上,通常目标 c 对我来说具有挑战性),所以我不知道如何排序.我正在尝试做这样的事情(伪代码,我用 actionscript 风格写这个,因为我对它更舒服):
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.
推荐答案
您想快速完成吗?
如果你有一个 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屋!