2

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?

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Shalin Shah
  • 8,145
  • 6
  • 31
  • 44
  • 1
    What have you tried so far? Have you tried out some books tutorials to get the basics down? – bryanmac Aug 28 '12 at 02:30
  • 1
    It's great that you want to learn Objective-C. For one thing, it's a rare skill which means that when you're an adult looking for a job, people will be willing to pay you more money. :) But you need to put effort into it yourself first, and that means reading every book you can find about C, Objective-C, and the concepts involved. (Oh, and take as many math classes as you can in high school and college/uni, especially algebra. Yes, you'll use algebra in the real world, I promise.) – Jonathan Grynspan Aug 28 '12 at 02:39
  • 2
    I have read a lot about NSMutableArrays and other Obective-C Documentaions and have searched a lot for answers before posting this. – Shalin Shah Aug 28 '12 at 03:17
  • 2
    @ShalinShah: This is a very basic question. Not to be rude, but you haven't read enough if you're still asking for help with this sort of thing. Keep reading. – Jonathan Grynspan Aug 28 '12 at 03:20

3 Answers3

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

}
Shalin Shah
  • 8,145
  • 6
  • 31
  • 44
  • @James the method compare: is called on the NSNumbers in the array. NSNumber declares the method in its interface. He cannot post the compare method implementation because Apple wrote it. See: - (NSComparisonResult)compare:(NSNumber *)otherNumber; from NSNumber: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html#//apple_ref/occ/instm/NSNumber/compare: – Kyle Robson Apr 03 '15 at 02:28
5

NSMutableArray has some great sorting methods, as documented here.

Unfortunately this is not a give me teh codez site - we want to see you put in a little effort yourself!

Luke
  • 7,110
  • 6
  • 45
  • 74
0

As Luke wrote, there are some great sorting methods built into NSMutableArray. You will learn a lot more if you implement the algorithms yourself, however. Check out Bubble Sort. It is one type of sorting algorithm that should serve you well in this task, and is a great thing to understand regardless.

EDIT: Check out this StackOverflow link. I literally just googled "sort NSMutableArray of NSNumbers" and this was one of the first hits.

Community
  • 1
  • 1
James
  • 2,272
  • 1
  • 21
  • 31
  • 1
    I would recommend against bubble sort. It's much harder to understand than a [`merge sort`](http://en.wikipedia.org/wiki/Merge_sort) or [`quick sort`](http://en.wikipedia.org/wiki/Quicksort), and bubble sort encourages you to sort things in the slowest possible way. – user1118321 Aug 28 '12 at 03:24
  • @user1118321 that's a fair point. Still not a bad thing to understand, imo. Either way, I think my basic sentiment that learning how it works at this point in the game is more valuable than being able to just make it work in one language. – James Aug 28 '12 at 03:29