美文网首页
数组排序和过滤

数组排序和过滤

作者: liboxiang | 来源:发表于2019-02-20 09:45 被阅读1次

排序

  • NSComparator
    NSOrderedAscending 表示 obj1 应该排在 obj2 前面
    NSOrderedDescending 表示 obj2 应该排在 obj1 前面
    NSOrderedSame 表示 obj1 和 obj2 是一样的
[testArray sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {  
        if ([obj1 intValue] < [obj2 intValue]) {  
            return NSOrderedAscending;  
        }else if ([obj1 intValue] > [obj2 intValue]){  
            return NSOrderedDescending;  
        }else{  
            return NSOrderedSame;  
        }  
    }]; 

[testArray sortedArrayWithOptions:<#(NSSortOptions)#> usingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
    }]
NSSortConcurrent 是高效的但不稳定的排序算法,例如:快速排序
NSSortStable 是稳定的排序算法,例如:冒泡排序 插入排序
如果使用 NSSortStable 正确的结果是 @"one", @"two", @"four", @"three"
如果使用 NSSortConcurrent 正确的结果是 @"one", @"two", @"four", @"three" 或者 @"two", @"one", @"four", @"three"
  • selector
    [[NSArray new] sortedArrayUsingSelector:<#(nonnull SEL)#>]
  • function
[[NSArray new] sortedArrayUsingFunction:<#(nonnull NSInteger (*)(id  _Nonnull __strong, id  _Nonnull __strong, void * _Nullable))#> context:<#(nullable void *)#>];
  • NSSortDescriptor
NSArray *firstNames = @[ @"Alice", @"Bob", @"Charlie", @"Quentin" ];
NSArray *lastNames = @[ @"Smith", @"Jones", @"Smith", @"Alberts" ];
NSArray *ages = @[ @24, @27, @33, @31 ];

NSMutableArray *people = [NSMutableArray array];
[firstNames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    Person *person = [[Person alloc] init];
    person.firstName = [firstNames objectAtIndex:idx];
    person.lastName = [lastNames objectAtIndex:idx];
    person.age = [ages objectAtIndex:idx];
    [people addObject:person];
}];

NSSortDescriptor *firstNameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"firstName"
  ascending:YES
  selector:@selector(localizedStandardCompare:)];
NSSortDescriptor *lastNameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"lastName"
  ascending:YES
  selector:@selector(localizedStandardCompare:)];
NSSortDescriptor *ageSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"age"
  ascending:NO];

NSLog(@"By age: %@", [people sortedArrayUsingDescriptors:@[ageSortDescriptor]]);
// "Charlie Smith", "Quentin Alberts", "Bob Jones", "Alice Smith"
NSLog(@"By first name: %@", [people sortedArrayUsingDescriptors:@[firstNameSortDescriptor]]);
// "Alice Smith", "Bob Jones", "Charlie Smith", "Quentin Alberts"
NSLog(@"By last name, first name: %@", [people sortedArrayUsingDescriptors:@[lastNameSortDescriptor, firstNameSortDescriptor]]);
// "Quentin Alberts", "Bob Jones", "Alice Smith", "Charlie Smith"

过滤

详情:http://nshipster.cn/nspredicate/

  • NSPredicate

  • NSCompoundPredicate

NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:<#(nonnull NSArray<NSPredicate *> *)#>];
[NSCompoundPredicate andPredicateWithSubpredicates:<#(nonnull NSArray<NSPredicate *> *)#>]
  • NSComparisonPredicate
    通过NSExpression生成predicate
  • NSExpression
    如果我们仔细观察NSPredicate,我们会发现它NSPredicate实际上由更小的原子部分组成:两个NSExpressionS(左手值右手值),与操作者(例如相比<,IN,LIKE等等)。
    详情:http://nshipster.com/nsexpression/

相关文章

  • 数组排序和过滤

    排序 NSComparatorNSOrderedAscending 表示 obj1 应该排在 obj2 前面NSO...

  • Vue数组过滤和排序

    ffilter方法 定义和用法 filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件...

  • JS数组方法速查

    1.数组去重 2.数组合并 3.数组排序(sort) 4.多维数组转一维数组(flat) 5.过滤数组(filte...

  • 不常用的一些方法的注意事项

    sort() 排序 用for-in 遍历数组过滤不符合的要求的key值 遍历数组 forEach() 为数组中含有...

  • 第五章 vueJS中的内置指令(下)

    5.4 数组更新,过滤与排序 改变数组的一系列方法: push() 在末尾添加元素 pop() 将数组的最后一个元...

  • 数组排序相关

    数组排序相关 结合sort和函数排序: 数组由小到大进行排序:sort,sortnum; vararr = [...

  • js_20 数组CRUD

    数组添加和删除 数组反转和排序

  • CPU占比优化小记

    场景: 高频300ms,数组分割,条件过滤,二维数组对象属性自定义排序,条件限制(相同累加,不足再次补位),刷新 ...

  • DAY. 05 冒泡排序,选择排序,杨辉三角

    学了一维数组的3种定义格式,数组的内存,遍历数组,数组的排序冒泡排序和选择排序,数组元素的查找,复制。 以及二维数...

  • iOS 各种排序

    数组排序 数组中字典排序 数组中字典按照某个value排序 排序方法

网友评论

      本文标题:数组排序和过滤

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