美文网首页
NSSortDescriptor

NSSortDescriptor

作者: 敏中 | 来源:发表于2020-12-03 10:42 被阅读0次

概述:NSSortDescriptor是用来指定排序规则, 对集合等进行排序时指定结果的排序规则;

他可以对一个类的某个属性(下文中方法中的key参数)指定排序规则, 也可以对一个字符串集合进行指定排序规则, 这时, 只需要把参数key赋值为nil即可。
iOS中的集合都有相应的扩展方法来使用NSSortDescriptor:

-(NSArray<ObjectType> *)sortedArrayUsingDescriptors:(NSArray<NSSortDescriptor *> *)sortDescriptors

具体的方法有:

1.

+ (instancetype)sortDescriptorWithKey:(nullable NSString *)key ascending:(BOOL)ascending NS_AVAILABLE(10_6, 4_0);

参数:
key : 排序key, 某个对象的属性名称
ascending : 是否升序, YES-升序, NO-降序

举一些例子:

        NSMutableArray *myMutableArr = [[NSMutableArray alloc] init] ;
        NSDictionary *dicOne = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"price",@"2",@"number", nil];
        NSDictionary *dicTWo = [NSDictionary dictionaryWithObjectsAndKeys:@"6",@"price",@"5",@"number", nil];
        NSDictionary *dicThree = [NSDictionary dictionaryWithObjectsAndKeys:@"3",@"price",@"1",@"number", nil];
        NSDictionary *dicFour = [NSDictionary dictionaryWithObjectsAndKeys:@"4",@"price",@"3",@"number", nil];
        [myMutableArr addObject:dicOne];
        [myMutableArr addObject:dicTWo];
        [myMutableArr addObject:dicThree];
        [myMutableArr addObject:dicFour];

        NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES]];
        [myMutableArr sortUsingDescriptors:sortDescriptors];
        NSLog(@"排序后的数组%@",myMutableArr);
         NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
         NSArray *descriptors = [NSArray arrayWithObject:descriptor];
         NSArray *reverseDayInMonthArray = [displayKeyDataArray sortedArrayUsingDescriptors:descriptors];

比较数字字符串和数字

    NSArray *array1 = @[@"5", @"7", @"9", @"12", @"35", @"1", @"3"];
    NSArray *array2 = @[@5, @6, @9, @1, @3, @19];
    NSSortDescriptor *ds1 = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES];
    NSArray *sortedArray1 = [array1 sortedArrayUsingDescriptors:@[ds1]];
    NSLog(@"sortedArray1:%@",sortedArray1);
    NSArray *sortedArray2 = [array2 sortedArrayUsingDescriptors:@[ds1]];
    NSLog(@"sortedArray2:%@",sortedArray2);

输出结果:

 sortedArray1:[
          "1",
           "12",
           "3",
           "35",
           "5",
           "7",
           "9"
        ]
sortedArray2:[
        1,
        3,
        5,
        6,
        9,
        19
      ]

NSMutableArray比较

   NSMutableArray *array3 = [@[@5, @6, @9, @1, @3, @19] mutableCopy];
    [array3 sortUsingDescriptors:@[ds1]];
    NSLog(@"sortedArray3:%@",array3);
输出结果:
sortedArray3:[
  1,
  3,
  5,
  6,
  9,
  19
]

比较NSDate数组

    NSArray *timeArray = @[[NSDate getSGDateByString:@"2020-12-02 14:00:00" format:@"yyyy-MM-dd HH:mm:ss"], [NSDate getSGDateByString:@"2020-12-12 14:00:00" format:@"yyyy-MM-dd HH:mm:ss"], [NSDate getSGDateByString:@"2020-11-22 14:00:00" format:@"yyyy-MM-dd HH:mm:ss"], [NSDate getSGDateByString:@"2020-12-02 14:00:01" format:@"yyyy-MM-dd HH:mm:ss"]];
    NSSortDescriptor *ds2 = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES];
    NSArray *sortedTimeArray = [timeArray sortedArrayUsingDescriptors:@[ds2]];
    NSLog(@"sortedTimeArray:%@",sortedTimeArray);
 输出结果:
sortedTimeArray:(
    "2020-11-22 14:00:00 +0000",
    "2020-12-02 14:00:00 +0000",
    "2020-12-02 14:00:01 +0000",
    "2020-12-12 14:00:00 +0000"
)

比较NSDictionary数组,根据字典中某个key的value值进行排序,使用两个条件进行排序

    NSArray *dicArray = @[@{@"name":@"1A", @"age":@14, @"score":@99.9}, @{@"name":@"1C", @"age":@14, @"score":@79.9}, @{@"name":@"1X", @"age":@12, @"score":@99.6}, @{@"name":@"1B", @"age":@4, @"score":@89.9}, @{@"name":@"2A", @"age":@12, @"score":@99}, @{@"name":@"3C", @"age":@14, @"score":@99.9}, @{@"name":@"1X", @"age":@15, @"score":@69.9}];
    NSSortDescriptor *ds3 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
    NSSortDescriptor *ds4 = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:NO];
    NSArray *sortedDicArray = [dicArray sortedArrayUsingDescriptors:@[ds4,ds3]];
    NSLog(@"sortedDicArray:%@",sortedDicArray);
 输出结果:
sortedDicArray:[
  {
    "name" : "1A",
    "age" : 14,
    "score" : 99.900000000000006
  },
  {
    "name" : "3C",
    "age" : 14,
    "score" : 99.900000000000006
  },
  {
    "name" : "1X",
    "age" : 12,
    "score" : 99.599999999999994
  },
  {
    "name" : "2A",
    "age" : 12,
    "score" : 99
  },
  {
    "name" : "1B",
    "age" : 4,
    "score" : 89.900000000000006
  },
  {
    "name" : "1C",
    "age" : 14,
    "score" : 79.900000000000006
  },
  {
    "name" : "1X",
    "age" : 15,
    "score" : 69.900000000000006
  }
]

比较NSDictionary数组,字典中某个key的value值为字典,根据子字典中某个key的value值进行排序

    NSArray *dicdicArray = @[@{@"name":@"1A", @"age":@14, @"score":@{@"yuwen":@87}}, @{@"name":@"1C", @"age":@14, @"score":@{@"yuwen":@79.9}}, @{@"name":@"1X", @"age":@12, @"score":@{@"yuwen":@99.6}}, @{@"name":@"1B", @"age":@4, @"score":@{@"yuwen":@89.9}}, @{@"name":@"2A", @"age":@12, @"score":@{@"yuwen":@99}}, @{@"name":@"3C", @"age":@14, @"score":@{@"yuwen":@99}}, @{@"name":@"1X", @"age":@15, @"score":@{@"yuwen":@69.9}}];
    NSSortDescriptor *ds5 = [NSSortDescriptor sortDescriptorWithKey:@"score.yuwen" ascending:YES];
    NSArray *sortedDicdicArray = [dicdicArray sortedArrayUsingDescriptors:@[ds5,ds3]];
    NSLog(@"sortedDicdicArray:%@",sortedDicdicArray);

输出结果:

sortedDicdicArray:[
  {
    "name" : "1X",
    "age" : 15,
    "score" : {
      "yuwen" : 69.900000000000006
    }
  },
  {
    "name" : "1C",
    "age" : 14,
    "score" : {
      "yuwen" : 79.900000000000006
    }
  },
  {
    "name" : "1A",
    "age" : 14,
    "score" : {
      "yuwen" : 87
    }
  },
  {
    "name" : "1B",
    "age" : 4,
    "score" : {
      "yuwen" : 89.900000000000006
    }
  },
  {
    "name" : "2A",
    "age" : 12,
    "score" : {
      "yuwen" : 99
    }
  },
  {
    "name" : "3C",
    "age" : 14,
    "score" : {
      "yuwen" : 99
    }
  },
  {
    "name" : "1X",
    "age" : 12,
    "score" : {
      "yuwen" : 99.599999999999994
    }
  }
]

2.

+ (instancetype)sortDescriptorWithKey:(nullable NSString *)key ascending:(BOOL)ascending selector:(nullable SEL)selector NS_AVAILABLE(10_6, 4_0);

参数:
key : 排序key, 某个对象的属性名称
ascending : 是否升序, YES-升序, NO-降序
selector : 自定义排序规则, 如果需要自己定义排序规则, 可传递此方法, 这个使用相对比较复杂; 如果待比较的属性是字符串(NSString)类型, 可使用其默认的方法: localizedStandardCompare:

3. 也可以使用NSComparisonResult,自己提供一个排序规则

+ (instancetype)sortDescriptorWithKey:(nullable NSString *)key ascending:(BOOL)ascending comparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);

参数:
key : 排序key, 某个对象的属性名称
ascending : 是否升序, YES-升序, NO-降序
cmptr: 一个block, 可以在其中指定比较规则
最终的排序顺序依赖 -> ascending和cmptr进行合并操作 升序为yes,降序为no,两者进行合并操作 NO、NO-> YES; YES、YES->YES; YES、NO->NO
//NSSortDescriptor中 + (instancetype)sortDescriptorWithKey:(nullable NSString *)key ascending:(BOOL)ascending comparator:(NSComparator)cmptr;
//[obj1 compare:obj2] 升序
//[obj2 compare:obj2] 降序
//ascending 与 comparator会进行合并操作 升序为yes,降序为no,两者进行合并操作 NO、NO-> YES; YES、YES->YES; YES、NO->NO

举一些例子:

    NSArray *dicArray = @[@{@"1A":@14}, @{@"1C":@12}, @{@"1X":@12}, @{@"1B":@4}, @{@"2A":@11}, @{@"3C":@9}, @{@"1X":@19}];
    NSSortDescriptor *ds3 = [NSSortDescriptor sortDescriptorWithKey:nil ascending:nil comparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [[obj1 allValues][0] compare:[obj2 allValues][0]];
    }];
    NSArray *sortedComparatorDicArray = [dicArray sortedArrayUsingDescriptors:@[ds3]];
    NSLog(@"Comparator - sortedDicArray:%@",sortedComparatorDicArray);// ascending:nil   NSComparisonResultYES -> NO 降序
Comparator - sortedDicArray:[
  {
    "1X" : 19
  },
  {
    "1A" : 14
  },
  {
    "1C" : 12
  },
  {
    "1X" : 12
  },
  {
    "2A" : 11
  },
  {
    "3C" : 9
  },
  {
    "1B" : 4
  }
]
    
    NSArray *dicdicArray = @[@{@"name":@"1A", @"age":@14, @"score":@{@"yuwen":@87}}, @{@"name":@"1C", @"age":@14, @"score":@{@"yuwen":@79.9}}, @{@"name":@"1X", @"age":@12, @"score":@{@"yuwen":@99.6}}, @{@"name":@"1B", @"age":@4, @"score":@{@"yuwen":@89.9}}, @{@"name":@"2A", @"age":@12, @"score":@{@"yuwen":@99}}, @{@"name":@"3C", @"age":@14, @"score":@{@"yuwen":@99}}, @{@"name":@"1X", @"age":@15, @"score":@{@"yuwen":@69.9}}];
    NSSortDescriptor *dsdic = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:NO comparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        NSNumber *num1 = [obj1 allValues][0];
        NSNumber *num2 = [obj2 allValues][0];
        return [num2 compare:num1];
    }];
    NSArray *sortedDicDicArray = [dicdicArray sortedArrayUsingDescriptors:@[dsdic]];
    NSLog(@"Comparator - sortedDicDicArray:%@",sortedDicDicArray);//ascending:NO NSComparisonResult NO -> NO 降序
    Comparator - sortedDicDicArray:[
  {
    "name" : "1X",
    "age" : 15,
    "score" : {
      "yuwen" : 69.900000000000006
    }
  },
  {
    "name" : "1C",
    "age" : 14,
    "score" : {
      "yuwen" : 79.900000000000006
    }
  },
  {
    "name" : "1A",
    "age" : 14,
    "score" : {
      "yuwen" : 87
    }
  },
  {
    "name" : "1B",
    "age" : 4,
    "score" : {
      "yuwen" : 89.900000000000006
    }
  },
  {
    "name" : "2A",
    "age" : 12,
    "score" : {
      "yuwen" : 99
    }
  },
  {
    "name" : "3C",
    "age" : 14,
    "score" : {
      "yuwen" : 99
    }
  },
  {
    "name" : "1X",
    "age" : 12,
    "score" : {
      "yuwen" : 99.599999999999994
    }
  }
]
    NSArray *dicindicArray = @[@{@"1A":@{@"yuwen":@87}}, @{@"1C":@{@"yuwen":@79.9}}, @{@"1X":@{@"yuwen":@99.6}}, @{@"1B":@{@"yuwen":@89.9}}, @{@"2A":@{@"yuwen":@99}}, @{@"3C":@{@"yuwen":@99}}, @{@"1X":@{@"yuwen":@69.9}}];
    NSSortDescriptor *dsdicindic = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO comparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [[[obj2 allValues][0] allValues][0] compare:[[obj1 allValues][0] allValues][0]];
    }];
    NSArray *sortedDicInDicArray = [dicindicArray sortedArrayUsingDescriptors:@[dsdicindic]];
    NSLog(@"Comparator - sortedDicInDicArray:%@",sortedDicInDicArray);//ascending:NO  NSComparisonResultNO-> NO 降序
    Comparator - sortedDicInDicArray:[
  {
    "1X" : {
      "yuwen" : 69.900000000000006
    }
  },
  {
    "1C" : {
      "yuwen" : 79.900000000000006
    }
  },
  {
    "1A" : {
      "yuwen" : 87
    }
  },
  {
    "1B" : {
      "yuwen" : 89.900000000000006
    }
  },
  {
    "2A" : {
      "yuwen" : 99
    }
  },
  {
    "3C" : {
      "yuwen" : 99
    }
  },
  {
    "1X" : {
      "yuwen" : 99.599999999999994
    }
  }
]
    NSSortDescriptor *dsNormal = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO comparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2];
    }];
    NSArray *array1 = @[@"5", @"7", @"9", @"12", @"35", @"1", @"3"];
    NSArray *array2 = @[@5, @6, @9, @1, @3, @19];
    NSArray *sortedArray1 = [array1 sortedArrayUsingDescriptors:@[dsNormal]];
    NSLog(@"Comparator - sortedArray1:%@",sortedArray1);
    NSArray *sortedArray2 = [array2 sortedArrayUsingDescriptors:@[dsNormal]];
    NSLog(@"Comparator - sortedArray2:%@",sortedArray2);//ascending:NO  NSComparisonResult YES-> NO降序
    Comparator - sortedArray1:[
  "9",
  "7",
  "5",
  "35",
  "3",
  "12",
  "1"
]
2020-12-04 10:46:05.448428+0800 InterTec[69682:1897614] Comparator - sortedArray2:[
  19,
  9,
  6,
  5,
  3,
  1
]
    
    NSMutableArray *tempArray = [NSMutableArray new];
    [dicArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [tempArray addObject:@{@"name":[obj allKeys][0], @"value":[obj allValues][0]}];
    }];
    
    NSSortDescriptor *ds4 = [NSSortDescriptor sortDescriptorWithKey:@"value" ascending:YES];
    NSArray *sortedDicArray = [tempArray sortedArrayUsingDescriptors:@[ds4]];
    NSLog(@"Comparator -  normal -sortedDicArray:%@",sortedDicArray);//ascending:YES 升序
    Comparator -  normal -sortedDicArray:[
  {
    "name" : "1B",
    "value" : 4
  },
  {
    "name" : "3C",
    "value" : 9
  },
  {
    "name" : "2A",
    "value" : 11
  },
  {
    "name" : "1C",
    "value" : 12
  },
  {
    "name" : "1X",
    "value" : 12
  },
  {
    "name" : "1A",
    "value" : 14
  },
  {
    "name" : "1X",
    "value" : 19
  }
]

    NSSortDescriptor *ds5 = [NSSortDescriptor sortDescriptorWithKey:@"value" ascending:NO comparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2]; 
    }];
    NSArray *sortedDicArray2 = [tempArray sortedArrayUsingDescriptors:@[ds5]];
    NSLog(@"Comparator - ! -sortedDicArray:%@",sortedDicArray2); //ascending:NO   NSComparisonResultYES,最终为NO,降序

Comparator - ! -sortedDicArray:[
  {
    "name" : "1X",
    "value" : 19
  },
  {
    "name" : "1A",
    "value" : 14
  },
  {
    "name" : "1C",
    "value" : 12
  },
  {
    "name" : "1X",
    "value" : 12
  },
  {
    "name" : "2A",
    "value" : 11
  },
  {
    "name" : "3C",
    "value" : 9
  },
  {
    "name" : "1B",
    "value" : 4
  }
]

相关文章

网友评论

      本文标题:NSSortDescriptor

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