g989_1314125 2012-12-05 02:19 采纳率: 80%
浏览 2431
已采纳

排序/过滤一个NSArray / NSMutableArray

如果定义了一个类:

MyClass
-------
NSString *name

然后放到数组里(或者可变数组),然后通过name条件筛选数组:

[NSPredicate predicateWithFormat:@"name = %@", someValue];

或者升序排序数组:

[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];

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

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

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

我知道可以用block。

  • 写回答

1条回答

  • Chengzi_963 2012-12-05 03:09
    关注

    当然可以用predicateWithFormat过滤string数组和其他类型的数组。排序用sortedArrayUsingSelector,selector任意(比如,比较可以用caseInsensitiveCompare)。

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services