码迷,mamicode.com
首页 > 移动开发 > 详细

【学习ios之路:Objective-C】OC中常用的系统排序方法

时间:2015-01-05 23:30:37      阅读:6303      评论:0      收藏:1      [点我收藏+]

标签:

①.OC中常用排序方法:

1).不可变数组

- (NSArray *)sortedArrayUsingSelector:(SEL)comparator;
- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr;
2)可变数组

- (void)sortUsingSelector:(SEL)comparator;
- (void)sortUsingComparator:(NSComparator)cmptr;

3).字典排序

- (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr 
- (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator;
②应用

1).不可变数组排序:(方法1)

   NSArray *arr = @[@"aa",@"rr",@"pp",@"hh",@"xx",@"vv"];
   //用系统的方法进行排序,系统缺少两个元素比较的方法.
   //selector方法选择器. 
   
    NSArray *sortArr = [arr sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@",sortArr);
   
方法2:block块语法

   [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [(NSString *)obj1 compare:(NSString *)obj2];
    }];
     NSLog(@"%@",arr);
    }
2).可变数组排序:方法1

    NSMutableArray *arr = [@[@54 ,@33,@12,@23,@65] mutableCopy];
    [arr sortUsingSelector:@selector(compare:)];//compare数组中两个元素比较的方法
    NSLog(@"%@",arr);
方法2
   [arr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [(NSNumber *)obj1 compare:(NSNumber *)obj2];
    }];
     NSLog(@"%@",arr);
    }
注:字典方法类似


③.例题:定义一个学生对象,对学生对象按照,姓名,年龄,成绩,学号进行排序,(两种排序方式)

方法1:(用selector方法选择器,需要重新定义comparator)

代码如下:

1.对象声明(student.h中)

@property (nonatomic, retain) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, assign) CGFloat score;
@property (nonatomic, assign) NSInteger number;

//初始化
- (id)initWithName:(NSString *)name age:(NSInteger)age score:
                                  (CGFloat)score number:(NSInteger)number;
//便利构造器
+ (id)studentWithName:(NSString *)name age:(NSInteger)age score:
                                   (CGFloat)score number:(NSInteger)number;

//两个学生按照年龄比较的方法
- (NSComparisonResult)compareByAge:(Student *)stu;

//两个学生按照姓名比较的方法
- (NSComparisonResult)compareByName:(Student *)stu;

//两个学生按照成绩比较的方法
- (NSComparisonResult)compareByScore:(Student *)stu;

//两个学生按照学号比较的方法
- (NSComparisonResult)compareByNumber:(Student *)stu;


 
2.实现(student.m文件)
 - (id)initWithName:(NSString *)name age:(NSInteger)age score:
                                        (CGFloat)score number:(NSInteger)number {

     self = [super init];
     if (self != nil) {
         self.name = name;
         self.age = age;
         self.score = score;
         self.number = number;
     }
     return self;
}
//便利构造器
+ (id)studentWithName:(NSString *)name age:(NSInteger)age score:
                                       (CGFloat)score number:(NSInteger)number {

    Student *student = [[Student alloc] initWithName:name
                                       age:age score:score number:number];
 
     return student;
}

//重写description
- (NSString *)description {
    return [NSString stringWithFormat:
                   @"name:%@,age:%ld,socre:%.1f,number:%ld",
                                  self.name, self.age, self.score, self.number];
}


//两个学生按照年龄比较的方法
- (NSComparisonResult)compareByAge:(Student *)stu {

    return [@(self.age) compare:@(stu.age)];//或者下面方法
    //return self.age > stu.age ? NSOrderedDescending
                    : self.age == stu.age ? NSOrderedSame : NSOrderedAscending;
}

//两个学生按照姓名降序的方法
- (NSComparisonResult)compareByName:(Student *)stu {
    return - [self.name compare:stu.name];
}

//两个学生按照成绩降序比较的方法
- (NSComparisonResult)compareByScore:(Student *)stu {

    return -[@(self.score) compare: (stu.score)];
    //return self.score < stu.score ? NSOrderedDescending :
                   self.score == stu.score ? NSOrderedSame : NSOrderedAscending;
     
}
 
//两个学生按照学号升序的方法
- (NSComparisonResult)compareByNumber:(Student *)stu {
 
      return [@(self.number) compare: (stu.number)];
     //return self.number > stu.number ? NSOrderedDescending 
              : self.number == stu.number ? NSOrderedSame : NSOrderedAscending;
}



主函数(main.h)

Student *student1 = [Student studentWithName:@"a" age:23 score:21 number:3434343];
Student *student2 = [Student studentWithName:@"b" age:45 score:432.4 number:324];
Student *student3 = [Student studentWithName:@"c" age:32 score:4321.4 number:2343];
Student *student4 = [Student studentWithName:@"d" age:7 score:43.4 number:233];
Student *student5 = [Student studentWithName:@"e" age:73 score:65 number:2332424];
        
NSMutableArray *arr = [NSMutableArray arrayWithObjects:
                           student1,student2,student3,student4,student5,nil];
      
        
  //按照年龄升序排序
  [arr sortUsingSelector:@selector(compareByAge:)];
  NSLog(@"%@",arr);
  //按照成绩降序排序
  [arr sortUsingSelector:@selector(compareByScore:)];
  NSLog(@"%@",arr);
  //按照姓名降序排序
  [arr sortUsingSelector:@selector(compareByName:)];
  NSLog(@"%@",arr);
  //按照学号升序排序
  [arr sortUsingSelector:@selector(compareByNumber:)];
  NSLog(@"%@",arr);
       
        
方法2.用block块语法进行排序

       //按年龄升序排序
        [arr sortUsingComparator:^(id obj1,id obj2) {
          return [@(((Student *)obj1).age) compare: @(((Student *)obj2).age)];
        }];
        NSLog(@"%@",arr);
        
        //按成绩降序排序
        [arr sortUsingComparator:^(id obj1,id obj2) {
           return [@(((Student *)obj1).score) compare: @(((Student *)obj2).score)];
        }];
        
        NSLog(@"%@",arr);
        //按姓名降序排序
        [arr sortUsingComparator:^(id obj1,id obj2) {
            return -[[(Student *)obj2 name] compare: [(Student *)obj1 name]];
        }];
        NSLog(@"%@",arr);
        
        //按学号升序排序
        NSComparator sortBlock = ^(id obj1,id obj2) {
           return [@(((Student *)obj1).number) compare: @(((Student *)obj2).number)];
        };
        [arr sortUsingComparator:sortBlock];
        NSLog(@"%@",arr);



【学习ios之路:Objective-C】OC中常用的系统排序方法

标签:

原文地址:http://blog.csdn.net/zfx5130/article/details/42433283

(1)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!