美文网首页
iOS 计算数组中的最大最小值,排序

iOS 计算数组中的最大最小值,排序

作者: th先生 | 来源:发表于2017-10-17 11:44 被阅读0次

现有如下数组:

NSArray *vals1 = @[@"1",@"3",@"5",@"8",@"8",@"8",@"8",@"8"];
NSArray *vals2 = @[@"2",@"4",@"6",@"8",@"6",@"6",@"6",@"6"];

1、通过valueForKeyPath方法

NSMutableArray *mutale = [[NSMutableArray alloc] init];
[mutale addObjectsFromArray:vals1];
[mutale addObjectsFromArray:vals2];
CGFloat maxValue = [[mutale valueForKeyPath:@"@max.floatValue"] floatValue];

@"@max.floatValue"(获取最大值),
@"@min.floatValue"(获取最小值),
@"@avg.floatValue" (获取平均值),
@"@count.floatValue"(获取数组大小)
@"@sum.floatValue"(数组的总和)

2、比较

NSMutableArray *mutale = [[NSMutableArray alloc] init];
[mutale addObjectsFromArray:vals1];
[mutale addObjectsFromArray:vals2];
NSSet *set = [NSSet setWithArray:mutale];
NSArray *arrary = [set allObjects];
    
NSInteger max = -100000000;
NSInteger min = 1000000000;
for (NSString *valueString in arrary) {
    NSInteger value = [valueString integerValue];
    if (value > max) {
        max = value;
    }
    if (value < min) {
        min = value;
    }
}

如果数组中有大量的重复值,我通过NSSet进行了一步过滤。
上面也可通过排序算法得到最大最小值

3、冒泡排序

NSMutableArray *mutale = [[NSMutableArray alloc] init];
[mutale addObjectsFromArray:vals1];
[mutale addObjectsFromArray:vals2];
for (int i =0; i<[mutale count]-1; i++) {
    for (int j =0; j<[mutale count]-1-i; j++) {
  //      if(([mutale[j] compare:mutale[j+1]]) == NSOrderedDescending)
          if([muti[j] integerValue] < [muti[j+1] integerValue] )
          {
            //交换
            [mutale exchangeObjectAtIndex:j withObjectAtIndex:j+1];
        }
    }
}

4、选择排序

NSMutableArray *mutale = [[NSMutableArray alloc] init];
[mutale addObjectsFromArray:vals1];
[mutale addObjectsFromArray:vals2];
for (int  i =0; i<[mutale count]-1; i++) {
    for (int j = i+1; j<[mutale count]; j++) {
        if ([mutale[i] intValue]>[mutale[j] intValue]) {
            //交换
            [mutale exchangeObjectAtIndex:i withObjectAtIndex:j];
        }
    }
}

5、升序降序

NSMutableArray *mutale = [[NSMutableArray alloc] init];
    [mutale addObjectsFromArray:vals1];
    [mutale addObjectsFromArray:vals2];
    NSSet *set = [NSSet setWithArray:mutale];
    NSArray *arrary = [set allObjects];
    
    //对数组进行排序
    NSArray *result = [arrary sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return[obj1 compare:obj2 options:NSNumericSearch];//数字在前,字母在后,排序
  //      return [obj1 compare:obj2]; //升序
   //     return [obj2 compare:obj1]; //降序
        
    }];
注意:经过实践,return [obj1 compare:obj2];或者return [obj2 compare:obj1];比如@[@"6",@"8",@"7",@"9",@"10"];这样的数组,升序降序都无法正常进行排序。慎用!这方法对于时间的排序还是很好用的。但是对于@[@"2017.2",@"2017.5",@"2017.11"];也无法正常排序

另外,以下方法也无法进行正常排序。

NSArray *result = [arrary sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        if ([obj1 intValue] > [obj2 intValue]){
                return NSOrderedAscending;
            }
        if ([obj1 intValue] < [obj2 intValue]){
            return NSOrderedAscending;
        }
        return NSOrderedSame;
        
    }];
NSArray *result = [arrary sortedArrayUsingSelector:@selector(compare:)];

下面是创建选择器,进行排序。对于上面方法的补充。

 NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self"
                                                               ascending:NO];
    NSArray *descriptors = [NSArray arrayWithObject:descriptor];
    NSArray *reverseOrder = [setArr sortedArrayUsingDescriptors:descriptors];

相关文章

网友评论

      本文标题:iOS 计算数组中的最大最小值,排序

      本文链接:https://www.haomeiwen.com/subject/oporuxtx.html