顺序整数升序和降序订单 [英] Order Integers In Ascending and Descending Orders
问题描述
我正与Objective-C的,但可能并不重要的编程语言这一点。所以基本上我有一个数组,也就是说,整数12,5,和17,而我希望能够拉出数量最多的,还是最小的,或第二小的,等等。
I'm working with Objective-C, but probably it doesn't matter the programming language for this. So basically I have an array, with say, the integers 12, 5, and 17, and I want to be able to pull out the largest number, or the smallest, or second smallest, etc.
基本上,我希望能够将它们分类到升序或降序排列,所以我可以挑出来,例如,第二小的数字通过检索 objectAtIndex:1
如果它是按升序排序。我觉得这是非常明显的,但我想不出如何在目前做到这一点,所以我会喜欢它,如果有人可以赐教。
Basically I want to be able to sort them into ascending or decending order so I could pick out, for instance, the second smallest number by retrieving the objectAtIndex: 1
if it is sorted in ascending order. I feel like this is incredibly obvious but I can't think of how to do it at the moment, so I would love it if someone could enlighten me.
推荐答案
如果你有一个的NSArray
与的NSNumber
实例,那么你正在寻找的排序是那么容易,因为这样的:
If you have an NSArray
with NSNumber
instances, then the sort you are looking for is as easy as this:
NSArray* sortedNumbers = [unorderedNumbers sortedArrayUsingSelector:@selector(intValue)];
这将排序上升,所以 [sortedNumbers lastObject]
将是最大的价值。
有关于的NSArray
更多的排序方法,如果你有更具体的要求。 <一href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/doc/uid/20000137-SW12"相对=nofollow>的NSArray排序
There are many more sorting methods on NSArray
if you have more specific needs. NSArray sorting
这篇关于顺序整数升序和降序订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!