iOS之NSArray数组排序
一、数组遍历
除了常用的for和for-in遍历外,系统还提供了三种枚举遍历,对于大量的数据遍历可以使用下列三个方法。
- (void)enumerateObjectsUsingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)); - (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)); - (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
定义一个数组,数据源如下:Xcode输出中文
2018-11-15 09:35:55.830699+0800 Test[2267:150185] ===( "name:往往0,age:0,sex:sex0", "name:往往1,age:1,sex:sex1", "name:往往7,age:7,sex:sex7", "name:往往2,age:2,sex:sex2", "name:往往3,age:3,sex:sex3", "name:往往4,age:4,sex:sex4" )
一、1 enumerateObjectsUsingBlock 数组正常枚举
[modelArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSLog(@"11111index=%ld, obj==%@",idx,obj); }];
效果:
2018-11-15 09:48:01.359039+0800 Test[2590:176418] 11111index=0, obj==name:往往0,age:0,sex:sex0 2018-11-15 09:48:01.359180+0800 Test[2590:176418] 11111index=1, obj==name:往往1,age:1,sex:sex1 2018-11-15 09:48:01.359298+0800 Test[2590:176418] 11111index=2, obj==name:往往7,age:7,sex:sex7 2018-11-15 09:48:01.359398+0800 Test[2590:176418] 11111index=3, obj==name:往往2,age:2,sex:sex2 2018-11-15 09:48:01.359491+0800 Test[2590:176418] 11111index=4, obj==name:往往3,age:3,sex:sex3 2018-11-15 09:48:01.359590+0800 Test[2590:176418] 11111index=5, obj==name:往往4,age:4,sex:sex4
一、2 enumerateObjectsWithOptions 指定排序方式(此排序只是对数组进行倒序枚举,并不是对数组里面的数据进行排序处理)
NSEnumerationOptions:枚举介绍
typedef NS_OPTIONS(NSUInteger, NSEnumerationOptions) { NSEnumerationConcurrent = (1UL << 0),//多线程来并发实现,并不保证按照顺序执行 NSEnumerationReverse = (1UL << 1),//倒序 };
BOOL * _Nonnull stop: 指定条件停止枚举:*stop = YES(YES表示暂停)
[modelArr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if (idx==4) { *stop = YES; } NSLog(@"2222index=%ld, obj==%@",idx,obj); }];
效果:倒序索引为4时暂停
2018-11-15 09:48:01.359790+0800 Test[2590:176418] 2222index=5, obj==name:往往4,age:4,sex:sex4 2018-11-15 09:48:01.359899+0800 Test[2590:176418] 2222index=4, obj==name:往往3,age:3,sex:sex3
一、3 enumerateObjectsAtIndexes 指定原数组范围
[modelArr enumerateObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)] options:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSLog(@"33333index=%ld, obj==%@",idx,obj); }];
效果:原数组范围(1,3)进行枚举
2018-11-15 09:55:54.010262+0800 Test[2702:191253] 33333index=3, obj==name:往往2,age:2,sex:sex2 2018-11-15 09:55:54.010399+0800 Test[2702:191253] 33333index=2, obj==name:往往7,age:7,sex:sex7 2018-11-15 09:55:54.010636+0800 Test[2702:191253] 33333index=1, obj==name:往往1,age:1,sex:sex1
二、数组中数据排序
NSSortDescriptor:设定规则,第二个参数ascending(YES表示降序排列,NO表示升序排列)
本例规则按照age:去数组每条数据的age键,按照对应键的值进行排序;(如果数组里封装的是字典形如 @[@{},@{}] 的形式这种排序就不适用了)
ascending:NO升序排列
sortUsingDescriptors(可变数组的分类)和sortedArrayUsingDescriptors(不可变数组分类)方法效果相同。
NSSortDescriptor *indexSD=[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO]; // NSMutableArray *temArr = [[modelArr sortedArrayUsingDescriptors:@[indexSD]] mutableCopy]; [modelArr sortUsingDescriptors:@[indexSD]]; NSLog(@"===%@",modelArr);
对上列数据按照年龄age进行排序效果如图:
2018-11-15 10:02:24.278353+0800 Test[2817:203857] ===( "name:往往0,age:0,sex:sex0", "name:往往1,age:1,sex:sex1", "name:往往7,age:7,sex:sex7", "name:往往2,age:2,sex:sex2", "name:往往3,age:3,sex:sex3", "name:往往4,age:4,sex:sex4" ) 2018-11-15 10:02:24.278669+0800 Test[2817:203857] ===( "name:往往7,age:7,sex:sex7", "name:往往4,age:4,sex:sex4", "name:往往3,age:3,sex:sex3", "name:往往2,age:2,sex:sex2", "name:往往1,age:1,sex:sex1", "name:往往0,age:0,sex:sex0" )
ForeverGuard博客园