NSArray 与 NSMutableArray 的排序
由于集合的使用過程中,經(jīng)常需要對數(shù)組進行排序操作,此博客用于總結(jié)對在OC中對數(shù)組排序的幾種方法
1.當數(shù)組中存放的是Foundation框架中提供的對象時,直接使用 compare:方法
? 如:NSString、NSMutableSting等
1 //使用塊對數(shù)組排序 2 NSArray* arr = @[@"4",@"3",@"1",@"2"]; 3 NSMutableArray* mutArr = [arr mutableCopy]; 4 5 //可變數(shù)組使用塊 sortUsingComparator 6 [mutArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { 7 return [obj1 compare: obj2]; 8 }]; 9 //不可變數(shù)組使用Comparator排序 10 NSArray* sorted = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { 11 return [obj2 compare: obj1]; 12 }]; 13 14 NSLog(@"%@", sorted); 15 NSLog(@"%@", mutArr); 16 17 //使用塊對數(shù)組進行排序 18 //可變數(shù)組使用selector排序 19 NSLog(@"%@", [arr sortedArrayUsingSelector:@selector(compare:)]);
2. 當數(shù)組中存放的是自定義對象時,需要自己寫排序方法,或使用NSSortDescriptor進行排序
舉個例子,自定義一個Student類,生成很多Student對象,放入數(shù)組中,對數(shù)組進行排序。
? ?2.1 Student類
1 #import <Foundation/Foundation.h> 2 @class Grade; 3 @interface Student : NSObject 4 @property (copy, nonatomic) NSString* name; 5 @property (assign, nonatomic) NSInteger age; 6 @property (copy, nonatomic) NSString* stuNo; 7 @property (strong, nonatomic) Grade* grade; 8 - (NSComparisonResult)compare:(Student *)stu; 9 10 - (NSString *)description; 11 12 @end 13 14 @implementation Student 15 - (NSComparisonResult)compare:(Student *)stu 16 { 17 return [_name compare: [stu name]]; 18 } 19 - (NSString *)description 20 { 21 return [NSString stringWithFormat:@"%@", _name]; 22 } 23 @end?2.2 排序 (使用自定義方法)
1 //對自定義對象進行排序 2 Grade* grade1 = [[Grade alloc] init]; 3 grade1.name = @"1023"; 4 Grade* grade2 = [[Grade alloc] init]; 5 grade2.name = @"1024"; 6 7 Student* stu1 = [[Student alloc] init]; 8 stu1.name = @"悟空"; 9 stu1.stuNo = @"008"; 10 stu1.age = 10; 11 stu1.grade = grade1; 12 Student* stu2 = [[Student alloc] init]; 13 stu2.name = @"八戒"; 14 stu2.stuNo = @"007"; 15 stu2.age = 12; 16 stu2.grade = grade2; 17 Student* stu3 = [[Student alloc] init]; 18 stu3.name = @"唐僧"; 19 stu3.stuNo = @"009"; 20 stu3.age = 14; 21 stu3.grade = grade2; 22 Student* stu4 = [[Student alloc] init]; 23 stu4.name = @"玉帝"; 24 stu4.stuNo = @"011"; 25 stu4.age = 16; 26 stu4.grade = grade1; 27 Student* stu5 = [[Student alloc] init]; 28 stu5.name = @"觀音"; 29 stu5.stuNo = @"112"; 30 stu5.age = 14; 31 stu5.grade = grade1; 32 33 NSArray* students = @[stu1, stu2, stu3, stu4,stu5]; 34 //對自定義對象排序需要自定義排序方法 35 NSArray* orderedArr = [students sortedArrayUsingSelector:@selector(compare:)]; 36 // NSLog(@"%@", orderedArr); 37 for (Student *new in orderedArr) { 38 NSLog(@"%@", new.name); 39 }?3.使用NSSortDescriptor進行排序,NSSortDescriptor排序更多的用于多條件排序
使用步驟:
1>創(chuàng)建NSSortDescriptor對象
1 //使用NSSortDescriptor對象描述某一條屬性的比較規(guī)則 2 //第一個參數(shù),是要比較的屬性所對應(yīng)的成員變量的名字 3 //第二個參數(shù),指定為YES, 表示為升序,指定為NO,表示為降序 4 5 NSSortDescriptor* desc1 = [[NSSortDescriptor alloc] initWithKey:@"_stuNo" ascending:YES]; 6 NSSortDescriptor* desc2 = [[NSSortDescriptor alloc] initWithKey:@"_age" ascending:YES]; 7 NSSortDescriptor* desc3 = [[NSSortDescriptor alloc] initWithKey:@"_name" ascending:YES]; 8 //【注】NSSortDescriptor 支持路徑,_grade為student對象的屬性對象,name為_grade的屬性 9 NSSortDescriptor* desc4 = [[NSSortDescriptor alloc] initWithKey:@"_grade.name" ascending:YES];?
2>將NSSortDescriptor對象放入數(shù)組
1 NSArray* descArr = [NSArray arrayWithObjects: desc4,desc1, desc3, desc2, nil];3>需要排序的數(shù)組調(diào)用 ?sortedArrayUsingDescriptors:方法進行排序
1 NSArray* sortedByDescriptor = [students sortedArrayUsingDescriptors: descArr];實例代碼如下:
Student.h文件
1 #import <Foundation/Foundation.h> 2 @class Grade; 3 @interface Student : NSObject 4 @property (copy, nonatomic) NSString* name; 5 @property (assign, nonatomic) NSInteger age; 6 @property (copy, nonatomic) NSString* stuNo; 7 @property (strong, nonatomic) Grade* grade; 8 - (NSComparisonResult)compare:(Student *)stu; 9 10 - (NSString *)description; 11 12 @endStudent.m文件
1 #import "Student.h" 2 3 @implementation Student 4 - (NSComparisonResult)compare:(Student *)stu 5 { 6 return [_name compare: [stu name]]; 7 } 8 - (NSString *)description 9 { 10 return [NSString stringWithFormat:@"%@", _name]; 11 } 12 @endGrade.h文件
1 #import <Foundation/Foundation.h> 2 3 @interface Grade : NSObject 4 @property (copy, nonatomic) NSString* name; 5 @endGrade.m文件
1 #import "Grade.h" 2 3 @implementation Grade 4 5 @end
主函數(shù):
1 //對自定義對象進行排序 2 Grade* grade1 = [[Grade alloc] init]; 3 grade1.name = @"1023"; 4 Grade* grade2 = [[Grade alloc] init]; 5 grade2.name = @"1024"; 6 7 Student* stu1 = [[Student alloc] init]; 8 stu1.name = @"悟空"; 9 stu1.stuNo = @"008"; 10 stu1.age = 10; 11 stu1.grade = grade1; 12 Student* stu2 = [[Student alloc] init]; 13 stu2.name = @"八戒"; 14 stu2.stuNo = @"007"; 15 stu2.age = 12; 16 stu2.grade = grade2; 17 Student* stu3 = [[Student alloc] init]; 18 stu3.name = @"唐僧"; 19 stu3.stuNo = @"009"; 20 stu3.age = 14; 21 stu3.grade = grade2; 22 Student* stu4 = [[Student alloc] init]; 23 stu4.name = @"玉帝"; 24 stu4.stuNo = @"011"; 25 stu4.age = 16; 26 stu4.grade = grade1; 27 Student* stu5 = [[Student alloc] init]; 28 stu5.name = @"觀音"; 29 stu5.stuNo = @"112"; 30 stu5.age = 14; 31 stu5.grade = grade1; 32 33 NSArray* students = @[stu1, stu2, stu3, stu4,stu5]; 34 35 NSSortDescriptor* desc1 = [[NSSortDescriptor alloc] initWithKey:@"_stuNo" ascending:YES]; 36 NSSortDescriptor* desc2 = [[NSSortDescriptor alloc] initWithKey:@"_age" ascending:YES]; 37 NSSortDescriptor* desc3 = [[NSSortDescriptor alloc] initWithKey:@"_name" ascending:YES]; 38 //可向下傳遞 39 NSSortDescriptor* desc4 = [[NSSortDescriptor alloc] initWithKey:@"_grade.name" ascending:YES]; 40 NSArray* descArr = [NSArray arrayWithObjects: desc4,desc1, desc3, desc2, nil]; 41 // NSArray* descArr = [NSArray arrayWithObjects:desc4, nil]; 42 NSArray* sortedByDescriptor = [students sortedArrayUsingDescriptors: descArr]; 43 44 for (Student *new in sortedByDescriptor) { 45 NSLog(@"%@", new.grade.name); 46 }?
轉(zhuǎn)載于:https://www.cnblogs.com/pretty-guy/p/3996374.html
總結(jié)
以上是生活随笔為你收集整理的NSArray 与 NSMutableArray 的排序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 30422元素
- 下一篇: linux 下oracle启动步骤