IOS 对数组内元素进行排序

Posted Somebody

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IOS 对数组内元素进行排序相关的知识,希望对你有一定的参考价值。

1、数组种元素为NSNumber类型,直接对数组种的元素进行排序:

#pragma mark -- 数组排序方法(升序)
- (void)arraySortASC{
    //数组排序
    //定义一个数字数组
    NSArray *array = @[@(3),@(4),@(2),@(1)];
    //对数组进行排序
    NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        NSLog(@"%@~%@",obj1,obj2); //3~4 2~1 3~1 3~2
        return [obj1 compare:obj2]; //升序
    }];
    NSLog(@"result=%@",result);
}
 
#pragma mark -- 数组排序方法(降序)
- (void)arraySortDESC{
    //数组排序
    //定义一个数字数组
    NSArray *array = @[@(3),@(4),@(2),@(1)];
    //对数组进行排序
    NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        NSLog(@"%@~%@",obj1,obj2); //3~4 2~1 3~1 3~2
        return [obj2 compare:obj1]; //降序
    }];
    NSLog(@"result=%@",result);
}
 
#pragma mark -- 数组排序方法(乱序)
- (void)arraySortBreak{
    //数组排序
    //定义一个数字数组
    NSArray *array = @[@(3),@(4),@(2),@(1),@(5),@(6),@(0)];
    //对数组进行排序
    NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        NSLog(@"%@~%@",obj1,obj2); 
        //乱序
        if (arc4random_uniform(2) == 0) {
            return [obj2 compare:obj1]; //降序
        }
        else{
            return [obj1 compare:obj2]; //升序
        }
    }];
    NSLog(@"result=%@",result);
}

2、数组内元素为对象,根据对象的某一属性进行排序:

#pragma mark -- 数组排序方法(升序)
- (void)arraySortASCWithKind:(KindType)kindType{

    //数组排序
    //定义一个数字数组
    NSArray *array = [NSArray arrayWithArray:self.dataMuArr];
    //对数组进行排序

    NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        NSLog(@"%@~%@",obj1,obj2); //3~4 2~1 3~1 3~2
        if ([obj1 isKindOfClass:[BATicker class]] && [obj2 isKindOfClass:[BATicker class]]) {
            BATicker * ticker1 = obj1;
            BATicker * ticker2 = obj2;
            NSNumber * number1 = @(ticker1.c);
            NSNumber * number2 =  @(ticker2.c);
            if (kindType == KindTypeTurnover) {//成交额
                number1 = @(ticker1.q);
                number2 =  @(ticker2.q);
            }else if (kindType == KindTypePrice) {//价格
                number1 = @(ticker1.c);
                number2 =  @(ticker2.c);
            }else if (kindType == KindTypePercentageGain) {//涨幅
                number1 = @(ticker1.price_P);
                number2 =  @(ticker2.price_P);
            }
            return [number1 compare:number2]; //升序
        }
        return [obj1 compare:obj2]; //升序
    }];
    NSMutableDictionary * keyIndexDic = [NSMutableDictionary dictionary];
    for (int i = 0; i < result.count; i++) {
        BATicker * ticker = result[i];
        [keyIndexDic setObject:@(i) forKey:ticker.s];
    }
    self.dataMuArr = [NSMutableArray arrayWithArray:result];
    self.keyIndexDic = [NSMutableDictionary dictionaryWithDictionary:keyIndexDic];
    [self.tableView reloadData];
    
    NSLog(@"result=%@",result);

}

#pragma mark -- 数组排序方法(降序)

- (void)arraySortDESCKind:(KindType)kindType{
    //数组排序
    //定义一个数字数组
    NSArray *array = [NSArray arrayWithArray:self.dataMuArr];
    //对数组进行排序

    NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        NSLog(@"%@~%@",obj1,obj2); //3~4 2~1 3~1 3~2
        if ([obj1 isKindOfClass:[BATicker class]] && [obj2 isKindOfClass:[BATicker class]]) {
            BATicker * ticker1 = obj1;
            BATicker * ticker2 = obj2;
            NSNumber * number1 = @(ticker1.c);
            NSNumber * number2 =  @(ticker2.c);
            if (kindType == KindTypeTurnover) {//成交额
                number1 = @(ticker1.q);
                number2 =  @(ticker2.q);
            }else if (kindType == KindTypePrice) {//价格
                number1 = @(ticker1.c);
                number2 =  @(ticker2.c);
            }else if (kindType == KindTypePercentageGain) {//涨幅
                number1 = @(ticker1.price_P);
                number2 =  @(ticker2.price_P);
            }
            return [number2 compare:number1]; //降序
        }
        return [obj2 compare:obj1]; //降序

    }];
    NSMutableDictionary * keyIndexDic = [NSMutableDictionary dictionary];
    for (int i = 0; i < result.count; i++) {
        BATicker * ticker = result[i];
        [keyIndexDic setObject:@(i) forKey:ticker.s];
    }
    self.dataMuArr = [NSMutableArray arrayWithArray:result];
    self.keyIndexDic = [NSMutableDictionary dictionaryWithDictionary:keyIndexDic];
    [self.tableView reloadData];

    NSLog(@"result=%@",result);

}

参考内容:
ios之数组的排序(升序、降序及乱序)【https://www.cnblogs.com/xiao-...

以上是关于IOS 对数组内元素进行排序的主要内容,如果未能解决你的问题,请参考以下文章

iOS 数组排序

基于struct内部的元素对指向struct的数组进行排序

在数组的线性时间内排序,每个元素至少出现n / 1000次

按字符串内的数字对数组进行排序[重复]

仅使用 3 个元素对 int 数组进行排序

在 nlog(n) 时间内对整数数组进行排序而不使用比较运算符