排序数目Objective-C

Sha*_*hah 2 arrays objective-c nslog nsnumber

我想制作一个演示应用程序,我可以在代码中输入4个数字,然后按1,2,3顺序对其进行排序NSLog.有简单的算法或方法吗?

Sha*_*hah 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);  

}
Run Code Online (Sandbox Code Playgroud)


Luk*_*uke 5

NSMutableArray有一些很好的排序方法,如此处所述.

不幸的是,这不是一个给我的代码网站 - 我们希望看到你自己付出一点努力!