NSArray NSDictionary排序

遍历
  1. for( id *obj in array )
  2. enumerateObjectsUsingBlock:^(){}
  3. NSEnumerator *
排序
  • NSArray /
  • NSArray< NSDictionary * > /
  • NSArray< ObjectType *>

不同的模式下,根据自己 ModelType 选择自定义或者系统排序处理;

< 1 >

    [array1 sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2]; 
    }];

< 2 >

    NSSortStable 串行操作稳定效率偏低;NSSortConcurrent 并行操作效率高,适合排序大规模数据;
    排序结果无异议!!!
    [array2 sortedArrayWithOptions:NSSortStable usingComparator:^NSComparisonResult(id obj1, id obj2){
     //sort in id-type
      int ,string ...
            }];

< 3 > 都是自定义不同的类型对比方法来进行排序;

    sortedArrayUsingFunction 
    sortedArrayUsingSelector

< 4 > NSSortDescriptor

      NSSortDescriptor *brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"xxx" ascending:YES];
      NSArray *sortDescriptors = [NSArray arrayWithObject:brandDescriptor];
     self.AllModelArray = [MyModelArray sortedArrayUsingDescriptors:sortDescriptors];

objectType 是字典或者自定义的Model,因为对于配置等处理结果都是无序的列表;
所以需要根据属性类型进行排序处理。<phoneNumber,country> 等;

More ->Search

NSPredicate

    NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"name beginswith[c] %@", filterString];
    self.visibleResults = [self.AllModelArray filteredArrayUsingPredicate:filterPredicate];

.........

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容