views:

840

answers:

2

I have an integer array in my Objective-C program. I'd like to sort it (ascending or descending, doesn't matter). In C++ I'd use the sort algorithm in the STL Algorithm library. How can I do this?

+4  A: 

The "Cocoa" way is to create NSNumber objects, add them to an NSArray, and use the sortUsingSelector:@selector(compare:) method (or one of the other NSArray sort methods).

If you want to create a C array of primitive NSInteger (int) values, you can use the built-in qsort(…) function of C.

Marc Charbonneau
+1  A: 

Even though this question has already been answered, I would like to add that you can also use STL collections with Objective-C++ and iPhone programming. Here is a link to a previous discussion on the topic.

Pablo Santa Cruz