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 NSNumber
s.
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/
精彩评论