Shalin Shah Shalin Shah - 4 months ago 25
Objective-C Question

Sorting Numbers Objective-C

I want to make a demo app where I can enter 4 numbers into the code and it sort it in 1,2,3 order and will

NSLog
it for me. Is there an easy algorithm or a way to do it?

Answer
// Put code in your App's ViewController
@implementation Sorting_NumbersViewController

- (void)viewDidLoad 
{

[super viewDidLoad];

// CODE STARTS HERE
  // This allocates and initializes the NSMutableArray
  NSMutableArray *anArray = [[NSMutableArray alloc] init];

  // These are where you enter your numbers
  [anArray addObject:@"1"];
  [anArray addObject:@"3"];
  [anArray addObject:@"2"];

  //This looks looks at the objects above and compares them with each-other
  NSArray *sorted = [anArray sortedArrayUsingSelector:@selector(compare:)];

  //This spits the result out in the console
  NSLog(@"Ordered Numbers: %@", sorted);  

}
Comments