标签:
NSArray数组的排序有三种方式:
1、简单排序(sortedArrayUsingSelector:)
2、利用block语法(sortedArrayUsingComparator:)
3、高级排序(sortedArrayUsingDescriptors:)
如果只是对字符串的排序,可以利用sortedArrayUsingSelector:方法就可以了,代码如下:
1 #pragma mark 数组排序 sortedArrayUsingSelector
2
3 void arraySort1()
4 {
5 NSArray *array = [NSArray arrayWithObjects:@"6",@"9",@"1",@"2",nil];
6 //返回一个排好序的数组,原来的数组不会变 (SEL)中传递的是排序的规则。@selector(compare:)
7 //制定元素的比较方法 compare
8 NSArray *arrayNew = [array sortedArrayUsingSelector:@selector(compare:)];
9 NSLog(@"arrayNew :%@",arrayNew);
10 }
这里是制定使用Compare方法进行排序。我们也可以制定自己的排序方法
1 #import <Foundation/Foundation.h>
2 @class Book;
3 @interface Student : NSObject
4 @property(nonatomic,retain)NSString *firstName;
5 @property(nonatomic,retain)NSString *lastName;
6 @property(nonatomic,retain)Book *book;
7 //快速创建student
8 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName;
9 //比较规则
10 - (NSComparisonResult)compareStudent:(Student*)stu;
11 @end
12
13 #import "Student.h"
14 #import "Book.h"
15 @implementation Student
16 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName
17 {
18 //类方法自动释放所以使用autorelease
19 Student *stu = [[[Student alloc]init]autorelease];
20 stu.firstName = firstName;
21 stu.lastName = lastName;
22 return stu;
23 }
24
25 - (NSComparisonResult)compareStudent:(Student*)stu
26 {
27 //1.先按照姓排序
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 - (NSString *)description
37 {
38 return [NSString stringWithFormat:@"[%@ %@]",self.lastName,self.firstName];
39 }
40
41 - (void)dealloc
42 {
43 [_book release];
44 [_firstName release];
45 [_lastName release];
46 [super dealloc];
47 }
48 @end
49
50
51 #pragma mark 数组排序 arraySort2
52 void arraySort2()
53 {
54 Student *stu = [Student studentWithFirstName:@"QY" lastName:@"Ma"];
55 Student *stu1 = [Student studentWithFirstName:@"jf" lastName:@"Jia"];
56 Student *stu2 = [Student studentWithFirstName:@"sy" lastName:@"Qu"];
57 NSArray *array = [NSArray arrayWithObjects:stu,stu1,stu2,nil];
58 //指定排序的方法
59 NSArray *arrayNew = [array sortedArrayUsingSelector:@selector(compareStudent:)];
60 NSLog(@"arrayNew : %@",arrayNew);
61
62 }
2.利用block语法(sortedArrayUsingComparator:)
这是苹果官方提供的block方法,其中数组排序可以用sortedArrayUsingComparator:方法
1 void arraySort3()
2 {
3 Student *stu = [Student studentWithFirstName:@"QY" lastName:@"Ma"];
4 Student *stu1 = [Student studentWithFirstName:@"jf" lastName:@"Jia"];
5 Student *stu2 = [Student studentWithFirstName:@"sy" lastName:@"Qu"];
6 NSArray *array = [NSArray arrayWithObjects:stu,stu1,stu2,nil];
7 //利用block进行排序
8 NSArray *arrayNew = [array sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {
9 //1.先按照姓排序
10 NSComparisonResult result = [obj1.lastName compare:obj2.lastName];
11
12 if(result == NSOrderedSame)
13 {
14 result = [obj1.firstName compare:obj2.firstName];
15 }
16 return result;
17 }];
18 NSLog(@"arrayNew : %@",arrayNew);
19 }
如果是这样一种情况呢?Student类里有另外一个类的变量,比如说Student类除了lastName,firstName变量,还有一本书,Book类里有个name属性。对Student对象进行排序,有这样的要求:按照Book的name排序,如果是同一本书,那么再按照lastName进行排序,如果lastName也相同,最后按照firstName进行排序。
代码如下:
1 //Book
2 #import <Foundation/Foundation.h>
3
4 @interface Book : NSObject
5 @property(nonatomic,retain)NSString *name;
6 + (id)bookWithName:(NSString*)name;
7 @end
8
9 #import "Book.h"
10
11 @implementation Book
12 + (id)bookWithName:(NSString*)name
13 {
14 Book *book = [[[Book alloc]init]autorelease];
15 book.name = name;
16 return book;
17
18
19 }
20 - (void)dealloc
21 {
22 [_name release];
23 [super dealloc];
24 }
25 @end
26
27 //Student
28 #import <Foundation/Foundation.h>
29 @class Book;
30 @interface Student : NSObject
31 @property(nonatomic,retain)NSString *firstName;
32 @property(nonatomic,retain)NSString *lastName;
33 @property(nonatomic,retain)Book *book;
34 //快速创建student
35 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName;
36 //快速创建student
37 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName bookName:(NSString*)bookName;
38 //比较规则
39 - (NSComparisonResult)compareStudent:(Student*)stu;
40 @end
41
42 #import "Student.h"
43 #import "Book.h"
44 @implementation Student
45 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName
46 {
47 //类方法自动释放所以使用autorelease
48 Student *stu = [[[Student alloc]init]autorelease];
49 stu.firstName = firstName;
50 stu.lastName = lastName;
51 return stu;
52 }
53
54
55 //快速创建student
56 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName bookName:(NSString*)bookName
57 {
58 Student *stu = [Student studentWithFirstName:firstName lastName:lastName];
59 stu.book = [Book bookWithName:bookName];
60 return stu;
61
62 }
63 - (NSComparisonResult)compareStudent:(Student*)stu
64 {
65 //1.先按照姓排序
66 NSComparisonResult result = [self.lastName compare:stu.lastName];
67
68 if(result == NSOrderedSame)
69 {
70 result = [self.firstName compare:stu.firstName];
71 }
72 return result;
73 }
74 - (NSString *)description
75 {
76 return [NSString stringWithFormat:@"[%@ %@ %@]",self.lastName,self.firstName,self.book.name];
77 }
78
79 - (void)dealloc
80 {
81 [_book release];
82 [_firstName release];
83 [_lastName release];
84 [super dealloc];
85 }
86 @end
87
88 //将排序描述按照顺序存入到数组中
89 #pragma mark 数组排序 sortedArrayUsingDescriptors 排序描述器
90 void arraySort5()
91 {
92 Student *stu1 = [Student studentWithFirstName:@"QY" lastName:@"Ma" bookName:@"book3"];
93 Student *stu2 = [Student studentWithFirstName:@"JF" lastName:@"Jia" bookName:@"book1"];
94 Student *stu3 = [Student studentWithFirstName:@"SY" lastName:@"Qu" bookName:@"book4"];
95 NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, nil];
96 //放入到描述器里面
97 NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
98 NSSortDescriptor *lastNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];
99 NSSortDescriptor *firstName = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];
100 NSArray *array1 = [NSArray arrayWithObjects:bookNameDesc,lastNameDesc,firstName,nil];
101 NSArray *arrayNew = [array sortedArrayUsingDescriptors:array1];
102 NSLog(@"arrayNew :%@",arrayNew);
103
104 }
标签:
原文地址:http://www.cnblogs.com/741162830qq/p/4427821.html