Hello,
Can anyone tell me how to sort an array. I have an array of number objects as per the code below and I'd like to sort them into numerical ascending order.
Can anyone tell me how to sort an array. I have an array of number objects as per the code below and I'd like to sort them into numerical ascending order.
Code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Set up some vars
int numbersPerLine = 6;
int maximumChoosableNumber = 49;
// Create a mutable array for holding all numbers in one line
NSMutableArray *line = [[NSMutableArray alloc] init];
// Seed the random number generator
srandom(time(NULL));
// Cycle through number choices for a line
for (int i=0; i<numbersPerLine; i++)
{
// Set a flag for checking duplicates
BOOL checker = false;
while (!checker) {
// Make my random number
int randomnumber = ((random()%maximumChoosableNumber)+1);
// Make a number object and init with the random number
NSNumber *num = [[NSNumber alloc] initWithInt:randomnumber];
// Check the number doesn't exit in my 'already chosen' numbers
if ([line indexOfObject:num] == NSNotFound)
{
// Add the new number to the array
[line addObject:num];
checker = true;
}
}
NSLog(@"Number %d is %@", i, [line objectAtIndex:i]);
}
// SORT THE ARRAY HERE
[pool drain];
return 0;
}