TOP

Objective-C 排序(一)
2017-10-12 11:12:01 】 浏览:10028
Tags:Objective-C 排序

在Objective-C中,排序分为:

1、Foundation框架中的对象排序

2、自定义对象排序

 例子:每个学生都有一个成绩score属性,根据成绩score对学生排序

 自定义对象 Student.h

Student.m

 

main.m

#import <Foundation/Foundation.h>
#import "Student.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
         //1、Foundation框架中的对象排序
         NSArray *arr = @[@12, @25, @15, @7, @18];
         NSLog(@"排序前: %@", arr);
         // 注意: 想使用compare方法对数组中的元素进行排序, 那么数组中的元素必须是Foundation框架中的对象, 也就是说不能是自定义对象
         NSArray *newArr = [arr sortedArrayUsingSelector:@selector(compare:)];
         NSLog(@"排序后: %@", newArr);
        
        //2、自定义对象排序
        
        Student *stu1 = [Student new];
        stu1.score = 91;
        
        Student *stu2 = [Student new];
        stu2.score = 97;
        
        Student *stu3 = [Student new];
        stu3.score = 95;
        
        Student *stu4 = [Student new];
        stu4.score = 87;
        
        
        NSArray *studentArr = @[stu1, stu2, stu3, stu4];
        NSLog(@"排序前: %@", studentArr);
        
        // 按照学生的成绩进行排序
        // 不能使用compare:方法对自定义对象进行排序
        // NSArray *newArr = [arr sortedArrayUsingSelector:@selector(compare:)];
        
        
        // 该方法默认会按照升序排序
        NSArray *newStudentArr = [studentArr sortedArrayWithOptions:NSSortStable usingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {
            //升序
            return obj1.score > obj2.score;
            //降序
//            return obj1.score < obj2.score;
        }];
        NSLog(@"成绩排序后: %@", newStudentArr);
        return 0;
    }
    return 0;
}

 结果:

 

3、自定义对象多个元素排序

JKStudent.h里面:

#import <Foundation/Foundation.h>

@interface JKStudent : NSObject

@property (nonatomic,assign) int age;
@property (nonatomic,retain) NSString *name;

-(id)initWithAge:(int)age andName:(NSString*)name;

+ (JKStudent *) studentWithAge:(int)age andName:(NSString *)name;


//排序规则
//比较年龄
-(NSComparisonResult)compare:(JKStudent*)otherStudent;
//比较姓名
-(NSComparisonResult)compareName:(JKStudent *)otherStudent;
//先年龄后姓名
-(NSComparisonResult)compareAgeAndName:(JKStudent *)otherStudent;


@end

 

JKStudent.m里面:

#import "JKStudent.h"

@implementation JKStudent

-(id)initWithAge:(int)age andName:(NSString*)name{
    self = [super init];
    if (self) {
        self.age = age;
        self.name = name;
    }
    return self;
}

+ (JKStudent *) studentWithAge:(int)age andName:(NSString *)name
{
    return [[JKStudent alloc]initWithAge:age andName:name];
}

-(NSString *)description{
    return [NSString stringWithFormat:@"age:%d name:%@",self.age,self.name];
}

//排序规则
//比较年龄
-(NSComparisonResult)compare:(JKStudent*)otherStudent{
    if(self.age>otherStudent.age){
        return NSOrderedDescending;
    }else if (self.age == otherStudent.age){
        return NSOrderedSame;
    }else{
        return NSOrderedAscending;
    }
}
//比较姓名
-(NSComparisonResult)compareName:(JKStudent *)otherStudent{
    return [self.name compare:otherStudent.name];
}

//先年龄后姓名
-(NSComparisonResult)compareAgeAndName:(JKStudent *)otherStudent{
    
    //先比较年龄
    if(self.age>otherStudent.age){
        return NSOrderedDescending;
    }else if (self.age == otherStudent.age){
        //比较姓名
        return [self.name compare:otherStudent.name];
    }else{
        return NSOrderedAscending;
    }
}

 

     //创建5个学生
        JKStudent *student1 = [JKStudent studentWithAge:12 andName:@"JACK"];
        JKStudent *student2 = [J  
		
Objective-C 排序(一) https://www.cppentry.com/bencandy.php?fid=99&id=146164

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS转场动画初探 下一篇OC学习笔记之属性详解和易错点