ios的NSArray排序

来源:互联网 发布:安全计算软件免费版 编辑:程序博客网 时间:2024/05/03 01:22

NSArray 排序的方法有很多中,,怕自己忘记了就把代码贴这里,方便日后查找。

Student.h

[cpp] view plaincopy
  1. //  
  2. //  Student.h  
  3. //  Foundation7-NSArray2  
  4. //  
  5. //  Created by mj on 13-4-5.  
  6. //  Copyright (c) 2013年 itcast. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @class Book;  
  12.   
  13. @interface Student : NSObject  
  14. @property (nonatomic, retain) NSString *firstname; // 名  
  15. @property (nonatomic, retain) NSString *lastname; // 姓  
  16. @property (nonatomic, retain) Book *book; // 书  
  17.   
  18. + (id)studentWithFirstname:(NSString *)firstname lastname:(NSString *)lastname;  
  19.   
  20. + (id)studentWithFirstname:(NSString *)firstname lastname:(NSString *)lastname bookName:(NSString *)bookName;  
  21.   
  22. // 返回值类型为NSComparisonResult  
  23. - (NSComparisonResult)compareStudent:(Student *)stu;  
  24. @end  

Student.m

[cpp] view plaincopy
  1. //  
  2. //  Student.m  
  3. //  Foundation7-NSArray2  
  4. //  
  5. //  Created by mj on 13-4-5.  
  6. //  Copyright (c) 2013年 itcast. All rights reserved.  
  7. //  
  8.   
  9. #import "Student.h"  
  10. #import "Book.h"  
  11.   
  12. @implementation Student  
  13.   
  14. + (id)studentWithFirstname:(NSString *)firstname lastname:(NSString *)lastname {  
  15.     Student *stu = [[[Student alloc] init] autorelease];  
  16.       
  17.     stu.lastname = lastname;  
  18.     stu.firstname = firstname;  
  19.       
  20.     return stu;  
  21. }  
  22.   
  23. + (id)studentWithFirstname:(NSString *)firstname lastname:(NSString *)lastname bookName:(NSString *)bookName {  
  24.     Student *stu = [Student studentWithFirstname:firstname lastname:lastname];  
  25.       
  26.     stu.book = [Book bookWithName:bookName];  
  27.       
  28.     return stu;  
  29. }  
  30.   
  31. - (NSComparisonResult)compareStudent:(Student *)stu {  
  32.     // 先按照姓排序  
  33.     NSComparisonResult result = [self.lastname compare:stu.lastname];  
  34.     // 如果有相同的姓,就比较名字  
  35.     if (result == NSOrderedSame) {  
  36.         result = [self.firstname compare:stu.firstname];  
  37.     }  
  38.       
  39.     return result;  
  40. }  
  41.   
  42. - (void)dealloc {  
  43.     [_firstname release];  
  44.     [_lastname release];  
  45.     [_book release];  
  46.     [super dealloc];  
  47. }  
  48.   
  49. - (NSString *)description {  
  50.     // Li MingeJie  
  51.       
  52.    return [NSString stringWithFormat:@"[%@ %@-%@]", self.lastname, self.firstname, self.book.name];  
  53. }  
  54. @end  

Book.h

[cpp] view plaincopy
  1. //  
  2. //  Book.h  
  3. //  Foundation7-NSArray2  
  4. //  
  5. //  Created by mj on 13-4-5.  
  6. //  Copyright (c) 2013年 itcast. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface Book : NSObject  
  12. @property (nonatomic, retain) NSString *name;  
  13.   
  14. + (id)bookWithName:(NSString *)name;  
  15. @end  


Book.m

[cpp] view plaincopy
  1. //  
  2. //  Book.m  
  3. //  Foundation7-NSArray2  
  4. //  
  5. //  Created by mj on 13-4-5.  
  6. //  Copyright (c) 2013年 itcast. All rights reserved.  
  7. //  
  8.   
  9. #import "Book.h"  
  10.   
  11. @implementation Book  
  12.   
  13. + (id)bookWithName:(NSString *)name {  
  14.     Book *book = [[[Book alloc] init] autorelease];  
  15.     book.name = name;  
  16.     return book;  
  17. }  
  18.   
  19. - (void)dealloc {  
  20.     [_name release];  
  21.     [super dealloc];  
  22. }  
  23. @end  

main.m

[cpp] view plaincopy
  1. //  
  2. //  main.m  
  3. //  Foundation7-NSArray2  
  4. //  
  5. //  Created by mj on 13-4-5.  
  6. //  Copyright (c) 2013年 itcast. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "Student.h"  
  11.   
  12. #pragma mark 派生出新的数组  
  13. void arrayNew() {  
  14.     NSArray *array = [NSArray arrayWithObjects:@"1", @"2", nil];  
  15.       
  16.     NSArray *array2 = [array arrayByAddingObject:@"3"];  
  17.       
  18.     NSArray *array3 = [array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"4", @"5", nil]];  
  19.       
  20.     NSLog(@"array:%@", array);  
  21.     NSLog(@"array2:%@", array2);  
  22.     NSLog(@"array3:%@", array3);  
  23.       
  24.       
  25.     NSArray *array4 = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];  
  26.     NSRange range = NSMakeRange(1, 2);  
  27.     NSArray *array5 = [array4 subarrayWithRange:range];  
  28.     NSLog(@"array5:%@", array5);  
  29. }  
  30.   
  31. #pragma mark 数组的其他用法  
  32. void arrayOther() {  
  33.     NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];  
  34.     // 1-2-3-4  
  35.     // 利用分隔符-拼接所有的数组元素  
  36.     NSString *str = [array componentsJoinedByString:@"-"];  
  37.     NSLog(@"%@", str);  
  38.       
  39.     // 将一个数组写入文件(生成的是一个xml文件)  
  40.     NSString *path = @"/Users/apple/Desktop/array.xml";  
  41.     [array writeToFile:path atomically:YES];  
  42.       
  43.       
  44.     path = @"/Users/apple/Desktop/array.txt";  
  45.     // 从文件中读取数组内容(文件有严格的格式要求)  
  46.     NSArray *array2 = [NSArray arrayWithContentsOfFile:path];  
  47.     NSLog(@"array2:%@", array2);  
  48. }  
  49.   
  50. #pragma mark 数组排序1  
  51. void arraySort1() {  
  52.     NSArray *array = [NSArray arrayWithObjects:@"2", @"3", @"1", @"4", nil];  
  53.       
  54.     // 返回一个排好序的数组,原来数组的元素顺序不会改变  
  55.     // 指定元素的比较方法:compare:  
  56.     NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];  
  57.     NSLog(@"array2:%@", array2);  
  58. }  
  59.   
  60. #pragma mark 数组排序2  
  61. void arraySort2() {  
  62.     Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];  
  63.     Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"];  
  64.     Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];  
  65.     Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"];  
  66.     NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];  
  67.       
  68.     // 指定排序的比较方法  
  69.     NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)];  
  70.       
  71.     NSLog(@"array2:%@", array2);  
  72. }  
  73.   
  74. #pragma mark 数组排序3  
  75. void arraySort3() {  
  76.     Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];  
  77.     Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"];  
  78.     Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];  
  79.     Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"];  
  80.     NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];  
  81.       
  82.     // 利用block进行排序  
  83.     NSArray *array2 = [array sortedArrayUsingComparator:  
  84.      ^NSComparisonResult(Student *obj1, Student *obj2) {  
  85.          // 先按照姓排序  
  86.          NSComparisonResult result = [obj1.lastname compare:obj2.lastname];  
  87.          // 如果有相同的姓,就比较名字  
  88.          if (result == NSOrderedSame) {  
  89.              result = [obj1.firstname compare:obj2.firstname];  
  90.          }  
  91.            
  92.          return result;  
  93.     }];  
  94.       
  95.     NSLog(@"array2:%@", array2);  
  96. }  
  97.   
  98. #pragma mark 数组排序4-高级排序  
  99. void arraySort4() {  
  100.     Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li" bookName:@"book1"];  
  101.     Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang" bookName:@"book2"];  
  102.     Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li" bookName:@"book2"];  
  103.     Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao" bookName:@"book1"];  
  104.     NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];  
  105.       
  106.     // 1.先按照书名进行排序  
  107.     // 这里的key写的是@property的名称  
  108.     NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];  
  109.     // 2.再按照姓进行排序  
  110.     NSSortDescriptor *lastnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];  
  111.     // 3.再按照名进行排序  
  112.     NSSortDescriptor *firstnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];  
  113.     // 按顺序添加排序描述器  
  114.     NSArray *descs = [NSArray arrayWithObjects:bookNameDesc, lastnameDesc, firstnameDesc, nil];  
  115.       
  116.     NSArray *array2 = [array sortedArrayUsingDescriptors:descs];  
  117.       
  118.     NSLog(@"array2:%@", array2);  
  119. }  
  120.   
  121. int main(int argc, const char * argv[])  
  122. {  
  123.   
  124.     @autoreleasepool {  
  125.         arraySort4();  
  126.     }  
  127.     return 0;  
0 0