NSArray排序

main函数:

  1 //   2 //  main.m   3 //  NSArray排序   4 //   5 //  Created by dingxiaowei on 13-5-13.   6 //  Copyright (c) 2013年 dingxiaowei. All rights reserved.   7 //   8    9 #import <Foundation/Foundation.h>  10 #import "Student.h"  11 #pragma mark - 派生出新数组  12 void newArray(){  13     NSArray *array1=[NSArray arrayWithObjects:@"1",@"2", nil];  14     NSArray *array2=[array1 arrayByAddingObject:@"3"];//添加元素  15     NSLog(@"array1:%@\narray2:%@",array1,array2);  16   17     NSArray *array3=[array1 arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"4",@"5",@"6",nil]]; //将后面一个array附加z在array1的前面  18     NSLog(@"array3:%@",array3);  19   20     //截取array中的元素  21     NSRange range=NSMakeRange(2, 2);//从第三个位置截取两个范围  22     NSArray *array4=[array3 subarrayWithRange:range];//截取数组元素  23     NSLog(@"截取后的数组是:%@",array4);  24   25       26 }  27   28 #pragma mark - 数组的其他用法  29 void arrayOther(){  30     NSArray * array=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",nil];  31     //用符号将数组拼接成字符串  32     NSString *str=[array componentsJoinedByString:@","];  33     NSLog(@"str=%@",str);  34   35     //将数组写入到文件  36     NSString *path=@"/Users/dingxiaowei/Desktop/array.xml";  37     [array writeToFile:path atomically:YES];//原子性就是等全部加载到文件中再写入  38     NSLog(@"文件写入成功");  39     //从文件中读取内容(文件格式由严格的要求)  40     NSArray *arrayRead=[NSArray arrayWithContentsOfFile:path];  41     NSLog(@"读取文件的内容:%@",arrayRead);  42     NSLog(@"文件读取成功");  43 }  44   45 #pragma mark - 数组排序  46 void arraySort1(){  47     NSArray * array=[NSArray arrayWithObjects:@"5",@"2",@"3",@"4",nil];  48     //指定元素的比较方法 数组中元素之间diao  49     NSArray *newArray=[array sortedArrayUsingSelector:@selector(compare:)];//返回一个新的排序后的数组,原来的那个数组不可变   依次调用compare方法  50     NSLog(@"排序后的数组:%@",newArray);  51 }  52 #pragma mark - 数组排序2(对对象进行排序)  53 void arraySort2(){  54     Student *stu1=[Student initeWithFirstName:@"xiaowei" andLastName:@"ding"];  55     Student *stu2=[Student initeWithFirstName:@"lianjie" andLastName:@"li"];  56     Student *stu3=[Student initeWithFirstName:@"xiaolong" andLastName:@"ding"];  57     Student *stu4=[Student initeWithFirstName:@"pengyu" andLastName:@"han"];  58       59     NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4,nil];  60     //按照学生的姓名进行排序  61     NSArray *newArray=[array sortedArrayUsingSelector:@selector(compareStudent:)];  62     NSLog(@"排序后的学生数组是:%@",newArray);  63 }  64 #pragma mark - block排序  65 void arraySort3(){  66     Student *stu1=[Student initeWithFirstName:@"xiaowei" andLastName:@"ding"];  67     Student *stu2=[Student initeWithFirstName:@"lianjie" andLastName:@"li"];  68     Student *stu3=[Student initeWithFirstName:@"xiaolong" andLastName:@"ding"];  69     Student *stu4=[Student initeWithFirstName:@"pengyu" andLastName:@"han"];  70   71     NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4,nil];  72     //利用block进行排序  73     NSArray *newArray=[array sortedArrayUsingComparator:^NSComparisonResult(Student * obj1, Student * obj2) {  74         //先按照姓氏排序  75         NSComparisonResult *result=[obj1.lastName compare:obj2.lastName];  76         //如果由相同的姓,则比较名  77         if(result==NSOrderedSame)  78         {  79             result=[obj1.firstName compare:obj2.firstName];  80         }  81         return result;  82     }];  83     NSLog(@"排序后的学生数组是:%@",newArray);  84 }  85 #pragma mark - 带有Book类的学生进行排序  86 void arraySort4(){  87     Student *stu1=[Student initeWithFirstName:@"xiaowei" andLastName:@"ding" andBookName:@"book1"];  88     Student *stu2=[Student initeWithFirstName:@"lianjie" andLastName:@"li" andBookName:@"book2"];  89     Student *stu3=[Student initeWithFirstName:@"xiaolong" andLastName:@"ding" andBookName:@"book2"];  90     Student *stu4=[Student initeWithFirstName:@"pengyu" andLastName:@"han" andBookName:@"book1"];  91   92     NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4,nil];  93     //1.先按照书名进行排序  94     //排序描述类  95     //先按照书进行排序  96     NSSortDescriptor *bookNameDes=[NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];//YES代表升序   第一个参数跟类的成员名要一致 这儿写成book.name是因为Student对象本身就有个Book*类型的book成员,然而book又有个name属性  97     //再按照姓进行排序  98     NSSortDescriptor *lastNameDes=[NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];//YES代表升序    //这儿的lastName要跟前面的成员名一致及propert里面的lastName一致  99     //再按照名进行排序 100     NSSortDescriptor *firstNameDes=[NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];//YES代表升序 101  102     //类名排序类 103     NSArray *desc=[NSArray arrayWithObjects:bookNameDes,lastNameDes,firstNameDes,nil]; 104     NSArray *array2=[array sortedArrayUsingDescriptors:desc];//按照那个排序类里面的成员依次进行排序 105     NSLog(@"未排序之前的成员顺序是:%@",array); 106     NSLog(@"排序后的成员(先按书名排序,后按照姓名排序):%@",array2); 107 } 108 int main(int argc, const char * argv[]) 109 { 110  111     @autoreleasepool { 112          113 //        newArray(); 114 //        arrayOther(); 115 //        arraySort1(); 116 //        arraySort2(); 117 //        arraySort3(); 118         arraySort4(); 119     } 120     return 0; 121 }

Student.h

 1 #import <Foundation/Foundation.h>  2   3 @class Book;  4   5   6 @interface Student : NSObject  7 @property(nonatomic,retain) NSString *firstName; //  8 @property(nonatomic,retain) NSString *lastName; //  9 @property(nonatomic,retain) Book *book;//这是一本书 10 //初始化带有姓名的学生 11 +(id)initeWithFirstName:(NSString *) firstName andLastName:(NSString *)lastName; 12 //初始化一个拥有书名且由姓名的学生 13 +(id)initeWithFirstName:(NSString *) firstName andLastName:(NSString *)lastName andBookName:(NSString *)bookName; 14 //设置一个比较函数 返回值为NSComparisonResult 15 -(NSComparisonResult)compareStudent:(Student *)stu; 16 @end

Student.m

 1 //  2 //  Student.m  3 //  NSArray排序  4 //  5 //  Created by dingxiaowei on 13-5-13.  6 //  Copyright (c) 2013年 dingxiaowei. All rights reserved.  7 //  8   9 #import "Student.h" 10 #import "Book.h" 11 @implementation Student 12 //初始化带有姓名的学生 13 +(id)initeWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName{ 14     Student *stu=[[[Student alloc] init] autorelease]; 15     stu.firstName=firstName; 16     stu.lastName=lastName; 17     return stu; 18 } 19 //初始化一个拥有书名且由姓名的学生 20 +(id)initeWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName andBookName:(NSString *)bookName{ 21     Student *stu=[Student initeWithFirstName:firstName andLastName:lastName]; 22     stu.book=[Book bookWithName:bookName]; 23     return stu; 24 } 25 //实现比较方法 26 -(NSComparisonResult)compareStudent:(Student *)stu{ 27     //先按照姓氏排序 28     NSComparisonResult *result=[self.lastName compare:stu.lastName]; 29     //如果由相同的姓,则比较名 30     if(result==NSOrderedSame) 31     { 32         result=[self.firstName compare:stu.firstName]; 33     } 34     return result; 35 } 36 //重新实现description方法 37 -(NSString *)description{ 38     return [NSString stringWithFormat:@"[%@ %@-%@]",self.lastName,self.firstName,self.book.name]; 39 } 40 -(void)dealloc{ 41     [_firstName release]; 42     [_lastName release]; 43      44     [super dealloc]; 45 } 46 @end

Book.h

1 #import <Foundation/Foundation.h> 2  3 @interface Book : NSObject 4 @property(nonatomic,retain)NSString *name; 5 +(id)bookWithName:(NSString *)name; 6 @end

Book.m

 1 #import "Book.h"  2   3 @implementation Book  4 //快速创建对象  5 +(id)bookWithName:(NSString *)name{  6     Book *book=[[[Book alloc] init] autorelease];  7     book.name=name;  8     return book;  9 } 10 //释放对象 11 -(void)dealloc{ 12     [_name release]; 13     [super dealloc]; 14 } 15 @end

运行结果

NSArray排序
 1 2013-05-14 21:21:22.339 NSArray排序[492:303] 未排序之前的成员顺序是:(  2     "[ding xiaowei-book1]",  3     "[li lianjie-book2]",  4     "[ding xiaolong-book2]",  5     "[han pengyu-book1]"  6 )  7 2013-05-14 21:21:22.345 NSArray排序[492:303] 排序后的成员(先按书名排序,后按照姓名排序):(  8     "[ding xiaowei-book1]",  9     "[han pengyu-book1]", 10     "[ding xiaolong-book2]", 11     "[li lianjie-book2]" 12 )





















,如需转载请自行联系原作者

上一篇:javascript中遇到的字符串对象处理


下一篇:C中使用errno查看函数调用的错误