开发者

Simple question about simple arrays in Objective-C

OK this is probably a really stupid question but I can't seem to find t开发者_JAVA百科he answer.

I know how to sort arrays using sort descriptors but what about a simple array containing only a series of numbers?

For example an array containing the following numbers: 21,3,11,58,32,76,19,45,7,92

I just need the numbers in ascending order.


I'm assuming you mean an NSArray of NSNumbers.

Fairly simple:

NSArray *unorderedNumbers; // assume exists

NSArray *sortedArray = [unorderedNumbers
               sortedArrayUsingSelector:@selector(compare:)];

(See sortedArrayUsingSelector:).

This causes the objects (which happen to be numbers) to be compared using compare:.


The C standard library includes qsort

See: http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜