我如何获取 NSArray 最大对象

发布于 2024-12-11 17:58:47 字数 957 浏览 0 评论 0原文

我有一个 NSArray ,数组是这样的数字;

NSArray *ratio = [NSArray arrayWithObjects:@"99",@"88",@"77", @"66", @"55", @"44",@"33", @"22",@"11",@"0",@"-11",@"-22",@"-33",@"-44",@"-55",@"-66",@"-77",@"-88",@"-99",@"-100",nil];

我想获取最大的物体 99 和最小的物体 -100 hiw 我可以这样做吗?

我将使用 if 条件最大的对象,如下所示;

if ([[ratio objectAtIndex:i] intValue] == 100 ) {

            rd = 0;
            gr = 0.5;
            bl =0;
}

//我想这样做

if ([[ratio objectAtIndex:i] intValue] == biggestobject or smallestobject ) {
      ///.....
}

请帮助我的朋友:( - 现在解决问题,但下面还有另一个问题。

第二个问题:(

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
NSLog(@"%@",[ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]]);

我对数组进行了排序,但无法从数组中选择最大和最小的对象。我该怎么做?

NSLog屏幕如下所示;

我如何在排序数组中获取 objectforkey:0

i have a NSArray and arrays are numbers like this;

NSArray *ratio = [NSArray arrayWithObjects:@"99",@"88",@"77", @"66", @"55", @"44",@"33", @"22",@"11",@"0",@"-11",@"-22",@"-33",@"-44",@"-55",@"-66",@"-77",@"-88",@"-99",@"-100",nil];

i want to take biggest object 99 and smallest object -100 hiw can i do this ?

i will use if condition biggest object like this ;

if ([[ratio objectAtIndex:i] intValue] == 100 ) {

            rd = 0;
            gr = 0.5;
            bl =0;
}

//i want to do this

if ([[ratio objectAtIndex:i] intValue] == biggestobject or smallestobject ) {
      ///.....
}

Help pls myfriends:( - Now solve problem but there is a another problem at follows.

Second one problem :(

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
NSLog(@"%@",[ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]]);

i sorted array but cannot choose biggest and smallest object from array. How can i do this?

NSLog screen is following like this; 99, 99, 99, 91, 91, 88, 88, 88, 88, 77, 77, 66, 66, 66,

how can i take objectforkey:0 at sorted array

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请止步禁区 2024-12-18 17:58:47

一种方法是对数组进行排序并获取第一个和最后一个索引。

按降序对 NSArray 进行排序

One way would be to just sort the array and take the first and last index.

Sort an NSArray in Descending Order

驱逐舰岛风号 2024-12-18 17:58:47

这会解决你的问题吗:

NSInteger maxValue = 0, minValue = 0;
BOOL firstIteration = YES;

for(id item in ratio) {
    NSInteger intValue = [item integerValue];
    if( firstIteration ) {
        maxValue = intValue;
        minValue = intValue;
        firstIteration = NO;
        continue;
    }

    if( intValue < minValue ) minValue = intValue;

    if( intValue > maxValue ) maxValue = intValue;
 }

will this solve your issue:

NSInteger maxValue = 0, minValue = 0;
BOOL firstIteration = YES;

for(id item in ratio) {
    NSInteger intValue = [item integerValue];
    if( firstIteration ) {
        maxValue = intValue;
        minValue = intValue;
        firstIteration = NO;
        continue;
    }

    if( intValue < minValue ) minValue = intValue;

    if( intValue > maxValue ) maxValue = intValue;
 }
荭秂 2024-12-18 17:58:47

编写此方法对您定义的两个字符串进行排序,请注意 NSNumericSearch请参阅此处的文档。

NSComparisonResult sortNums(NSString *s1, NSString *s2, void *context) {    
        return [s2 compare:s1 options:NSNumericSearch];
    }

然后使用以下命令进行排序:

NSArray *sorted = [unsorted sortedArrayUsingFunction:sortNums context:nil];

Write this method to sort two strings as you have defined, note the NSNumericSearch . See Documentation here.

NSComparisonResult sortNums(NSString *s1, NSString *s2, void *context) {    
        return [s2 compare:s1 options:NSNumericSearch];
    }

then sort using:

NSArray *sorted = [unsorted sortedArrayUsingFunction:sortNums context:nil];
[浮城] 2024-12-18 17:58:47

要获取值,请使用以下代码:

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
NSLog(@"%@",[ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]]);

您需要使用:

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO]; 
NSArray *sortedArray = [ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];

NSLog(@"max item: %@, min item: %@", [soretedArray objectAtIndex:0], [sortedArray lastObject]);

to get the values, instead the following code:

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
NSLog(@"%@",[ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]]);

you need to use:

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO]; 
NSArray *sortedArray = [ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];

NSLog(@"max item: %@, min item: %@", [soretedArray objectAtIndex:0], [sortedArray lastObject]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文