gpt4 book ai didi

objective-c - 对数字进行排序 Objective-C

转载 作者:行者123 更新时间:2023-12-02 21:51:31 25 4
gpt4 key购买 nike

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

最佳答案

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

}

关于objective-c - 对数字进行排序 Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12152020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com