[oc学习笔记]数组的遍历和排序
1 //数组的遍历(可变数组举例) 2 //for遍历 3 NSMutableArray *aa = [NSMutableArray array]; 4 [aa addObjectsFromArray:@[@"ss",@"ddd",@"sad"]]; 5 for (int i = 0; i < aa.count; i++) { 6 NSLog(@"%@",[aa objectAtIndex:i]); 7 } 8 //快速遍历 9 for (id obj in aa) { 10 NSLog(@"%@",obj); 11 } 12 //代码块遍历 13 // obj是对象 idx是下标 stop是停止标志 14 [aa enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 15 NSLog(@"%@",obj); 16 NSLog(@"%@",[aa objectAtIndex:idx]); 17 }]; 18 //迭代器遍历 19 //将数组元素存放进迭代器 20 NSEnumerator *en = [aa objectEnumerator]; 21 id obj = nil;//创建一个对象接受返回对象 22 while (obj = [en nextObject]) { 23 NSLog(@"%@",obj); 24 } 25 //反向迭代器遍历 26 NSEnumerator *en1 = [aa reverseObjectEnumerator]; 27 id obj1 = nil; 28 while (obj1 = [en1 nextObject]) { 29 NSLog(@"%@",obj1); 30 } 31 32 33 //排序(可变数组举例) 34 //可变数组排序的方法无返回值 不可变数组的排序有返回值 35 //选择器排序 36 //compare方法尽限于比较元素是字符时 ,如有另外的比较 请另写比较方法 返回值(NSComparisonResult) 37 [aa sortUsingSelector:@selector(compare:)]; 38 //代码块排序 39 //根据实际情况写判断方法 40 [aa sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { 41 return [obj1 compare:obj2]; 42 }]; 43 //比较描述器排序 44 //对多个条件进行排序比较便利 45 Person *p1 = [[Person alloc]init]; 46 p1.name = @"ss"; 47 p1.age = @"16"; 48 49 Person *p2 = [[Person alloc]init]; 50 p2.name = @"dd"; 51 p2.age = @"14"; 52 53 Person *p3 = [[Person alloc]init]; 54 p3.name = @"hh"; 55 p3.age = @"22"; 56 57 Person *p4 = [[Person alloc]init]; 58 p4.name = @"hh"; 59 p4.age = @"19"; 60 NSMutableArray *sss = [NSMutableArray arrayWithObjects:p1,p2,p3,p4,nil]; 61 NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; 62 NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES]; 63 NSArray *s = @[sort1,sort2]; 64 [sss sortUsingDescriptors:s]; 65 NSLog(@"%@",sss);
person.h
1 #import <Foundation/Foundation.h> 2 3 @interface Person : NSObject 4 @property (nonatomic,strong)NSString *name; 5 @property (nonatomic,strong)NSString *age; 6 @end
person.m
1 #import "Person.h" 2 3 @implementation Person 4 - (NSString *)description 5 { 6 return [NSString stringWithFormat:@"%@ %@", _name,_age]; 7 } 8 @end
运行结果分析:
2015-06-01 17:37:55.984 Array[2030:140148] ss 2015-06-01 17:37:55.985 Array[2030:140148] ddd 2015-06-01 17:37:55.985 Array[2030:140148] sad 2015-06-01 17:37:55.986 Array[2030:140148] ss 2015-06-01 17:37:55.986 Array[2030:140148] ddd 2015-06-01 17:37:55.986 Array[2030:140148] sad 2015-06-01 17:37:55.986 Array[2030:140148] ss 2015-06-01 17:37:55.986 Array[2030:140148] ss 2015-06-01 17:37:55.986 Array[2030:140148] ddd 2015-06-01 17:37:55.987 Array[2030:140148] ddd 2015-06-01 17:37:55.987 Array[2030:140148] sad 2015-06-01 17:37:55.987 Array[2030:140148] sad 2015-06-01 17:37:55.992 Array[2030:140148] ss 2015-06-01 17:37:55.992 Array[2030:140148] ddd 2015-06-01 17:37:55.992 Array[2030:140148] sad 2015-06-01 17:37:55.992 Array[2030:140148] sad 2015-06-01 17:37:55.992 Array[2030:140148] ddd 2015-06-01 17:37:55.993 Array[2030:140148] ss 2015-06-01 17:37:55.993 Array[2030:140148] ( "dd 14", "hh 19", "hh 22", "ss 16" )
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。