开发者社区> 问答> 正文

排序/过滤一个NSArray / NSMutableArray

如果定义了一个类:
`

MyClass

NSString *name
`

然后放到数组里(或者可变数组),然后通过name条件筛选数组:
[NSPredicate predicateWithFormat:@"name = %@", someValue];

或者升序排序数组:
[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];

问题:如果是一个string数组(或者是NSNumber),能不能用相同的格式?

例如数组:@[@"Cat", @"Bat", @"Dog", @"Cow"];

能不能用predicateWithFormat或者sortDescriptorWithKey来过滤和排序?

我知道可以用block

展开
收起
爵霸 2016-05-27 10:43:11 1807 0
1 条回答
写回答
取消 提交回答
  • 当然可以用predicateWithFormat过滤string数组和其他类型的数组。排序用sortedArrayUsingSelector,selector任意(比如,比较可以用caseInsensitiveCompare)

    在简单数组中没有关键字key,所以不能用sortDescriptorWithKey。

    2019-07-17 19:17:17
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载