您所在的位置:小祥子 » 编程 » IOS » 正文

[oc学习笔记]数组的遍历和排序

时间:2015-06-01 编辑:0error 来源:本站整理
         //数组的遍历(可变数组举例)
         //for遍历
         NSMutableArray *aa = [NSMutableArray array];
         [aa addObjectsFromArray:@[@"ss",@"ddd",@"sad"]];
         for (int i = 0;  i < aa.count; i++) {
             NSLog(@"%@",[aa objectAtIndex:i]);
         }
         //快速遍历
         for (id obj in aa) {
             NSLog(@"%@",obj);
         }
         //代码块遍历
         // obj是对象 idx是下标 stop是停止标志
         [aa enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
             NSLog(@"%@",obj);
             NSLog(@"%@",[aa objectAtIndex:idx]);
         }];
         //迭代器遍历
         //将数组元素存放进迭代器
         NSEnumerator *en = [aa objectEnumerator];
         id obj = nil;//创建一个对象接受返回对象
         while (obj = [en nextObject]) {
             NSLog(@"%@",obj);
         }
         //反向迭代器遍历
         NSEnumerator *en1 = [aa reverseObjectEnumerator];
         id obj1 = nil;
         while (obj1 = [en1 nextObject]) {
             NSLog(@"%@",obj1);
         }
         
         
         //排序(可变数组举例)
         //可变数组排序的方法无返回值 不可变数组的排序有返回值
         //选择器排序
         //compare方法尽限于比较元素是字符时 ,如有另外的比较 请另写比较方法 返回值(NSComparisonResult)
         [aa sortUsingSelector:@selector(compare:)];
         //代码块排序
         //根据实际情况写判断方法
         [aa sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
             return [obj1 compare:obj2];
         }];
         //比较描述器排序
         //对多个条件进行排序比较便利
         Person *p1 = [[Person alloc]init];
         p1.name = @"ss";
         p1.age = @"16";
         
         Person *p2 = [[Person alloc]init];
         p2.name = @"dd";
         p2.age = @"14";
         
         Person *p3 = [[Person alloc]init];
         p3.name = @"hh";
         p3.age = @"22";
         
         Person *p4 = [[Person alloc]init];
         p4.name = @"hh";
         p4.age = @"19";
         NSMutableArray *sss = [NSMutableArray arrayWithObjects:p1,p2,p3,p4,nil];
         NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
         NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
         NSArray *s = @[sort1,sort2];
         [sss sortUsingDescriptors:s];
         NSLog(@"%@",sss);

person.h

 #import <Foundation/Foundation.h>
 
 @interface Person : NSObject
 @property (nonatomic,strong)NSString *name;
 @property (nonatomic,strong)NSString *age;
 @end

person.m

 #import "Person.h"
 
 @implementation Person
 - (NSString *)description
 {
     return [NSString stringWithFormat:@"%@ %@", _name,_age];
 }
 @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"
)
关键词:学习 笔记 数组 排序