Shalin Shah Shalin Shah - 1 year ago 271
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

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

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

- (void)viewDidLoad 

[super viewDidLoad];

  // 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);  

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download