zoukankan      html  css  js  c++  java
  • NSArray 与 NSMutableArray 的排序

      由于集合的使用过程中,经常需要对数组进行排序操作,此博客用于总结对在OC中对数组排序的几种方法

    1.当数组中存放的是Foundation框架中提供的对象时,直接使用 compare:方法

      如:NSString、NSMutableSting等

     

     1         //使用块对数组排序
     2         NSArray* arr = @[@"4",@"3",@"1",@"2"];
     3         NSMutableArray* mutArr = [arr mutableCopy];
     4         
     5         //可变数组使用块 sortUsingComparator
     6         [mutArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
     7             return [obj1 compare: obj2];
     8         }];
     9         //不可变数组使用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         //使用块对数组进行排序
    18         //可变数组使用selector排序
    19         NSLog(@"%@", [arr sortedArrayUsingSelector:@selector(compare:)]);

    2. 当数组中存放的是自定义对象时,需要自己写排序方法,或使用NSSortDescriptor进行排序

    举个例子,自定义一个Student类,生成很多Student对象,放入数组中,对数组进行排序。

       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>创建NSSortDescriptor对象

    1      //使用NSSortDescriptor对象描述某一条属性的比较规则
    2         //第一个参数,是要比较的属性所对应的成员变量的名字
    3         //第二个参数,指定为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对象放入数组

    1 NSArray* descArr = [NSArray arrayWithObjects: desc4,desc1, desc3, desc2, nil];

      3>需要排序的数组调用  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 @end

      Student.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 @end

      Grade.h文件

    1 #import <Foundation/Foundation.h>
    2 
    3 @interface Grade : NSObject
    4 @property (copy, nonatomic) NSString* name;
    5 @end

      Grade.m文件

      

    1 #import "Grade.h"
    2 
    3 @implementation Grade
    4 
    5 @end

      主函数:

     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         }
  • 相关阅读:
    0x02 枚举、模拟、递推
    0x01 位运算
    bzoj3529: [Sdoi2014]数表
    bzoj5216: [Lydsy2017省队十连测]公路建设
    POJ1789Truck History
    最小生成树模板
    POJ1258Agri-Net
    POJ1860Currency Exchange(SPFA)
    POJ3083Children of the Candy Corn
    POJ2503Babelfish
  • 原文地址:https://www.cnblogs.com/pretty-guy/p/3996374.html
Copyright © 2011-2022 走看看