按降序对NSArray排序 [英] Sort an NSArray in Descending Order
问题描述
我有一个 NSArray
的 NSNumber
对象,我已经成功地按升序排序使用以下:
Hey guys, I have an NSArray
of NSNumber
objects that I have successfully sorted in ascending order using the following:
[myArray sortedArrayUsingSelector:@selector(compare:)]
但是,我需要以降序顺序排序。我认为 compare:
仅按升序排序。虽然我可以逆转 NSArray
,我很好奇是否有一个更简单或更有效的方式去做这个。
However, I need to sort this in descending order. I take it that compare:
only sorts in ascending order. While I can go about reversing the NSArray
, I am curious as to whether or not there is a more simpler or more effective way of going about doing this.
感谢!
EDIT :我找到这个问题提供了一个简单的方法来反向迭代 NSArray
:
EDIT: I found this question which provides a simple way to reverse iterate an NSArray
:
for (id someObject in [myArray reverseObjectEnumerator])
这很好,我想这是一个很好的简单的解决方案,但我很好奇是否有一种方法来指定排序
This works fine, I guess it's a nice simple solution, but I'm curious as to whether or not there is a way to specify sorting in descending order.
推荐答案
使用排序描述符
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self"
ascending: NO];
return [myArray sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
这篇关于按降序对NSArray排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!