====排序====:
- 只有可变数组NSMutableArray才能排序,下面是比较常用的一种方法:
两个步骤:
- 创建排序条件:NSSortDescriptor排序描述对象,即按照哪个字段/属性,排序(升序/降序),可以有多个排序条件
- 使用NSMutableArray的方法-(void)sortUsingDescriptors:(NSArray<NSSortDescriptor *> *)sortDescriptors
//valueOfAssets和employeeID是employees的两个属性(或者含返回值的方法)
//通过sortDescriptorWithKey创建NSSortDescriptor排序描述对象,实参是属性和排序方向(上升/下降)
NSSortDescriptor *voa = [NSSortDescriptor sortDescriptorWithKey:@"valueOfAssets" ascending:YES];
NSSortDescriptor *eid = [NSSortDescriptor sortDescriptorWithKey:@"employeeID" ascending:YES];
//通过sortUsingDescriptors方法,排序数组,可以有多个排序条件。
//:@[voa,eid]是一个NSArray
[employees sortUsingDescriptors:@[voa,eid]];
====过滤====:
- 对collection进行过滤,下面是比较常用的一种方法:
也是两个步骤:
- 创建过滤条件:断言NSPredicate:
例如:NSPredicate *predicate = [NSPredicate predicateWithFormat:@"holder.valueOfAssets > 700"];
- 然后有两个方法:
- filteredArrayUsingPredicate:返回一个过滤后的NSArray
例如:NSArray *toBeReclaimed = [allAssets filteredArrayUsingPredicate:predicate];
- filterUsingPredicate:直接处理原有数组
例如:NSMutableArray *toBeReclaimed = allAssets;
Objective-C:数组排序、过滤
最新推荐文章于 2022-08-04 18:01:23 发布