• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Objective-C排序

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

在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 = [JKStudent studentWithAge:10 andName:@"LUSON"];
        JKStudent *student3 = [JKStudent studentWithAge:13 andName:@"EASON"];
        JKStudent *student4 = [JKStudent studentWithAge:12 andName:@"LINA"];
        JKStudent *student5 = [JKStudent studentWithAge:11 andName:@"KATU"];
        
        //初始数组
        NSArray *studentArray1 = [NSArray arrayWithObjects:student1,student2,student3,student4,student5, nil];
        NSLog(@"初始数组:%@",studentArray1);
        
        //按照年龄排序
        NSArray *studentArray2 = [studentArray1 sortedArrayUsingSelector:@selector(compare:)];
        NSLog(@"按照年龄排序:%@",studentArray2);
        
        //按照名字排序
        NSArray *studentArray3 = [studentArray1 sortedArrayUsingSelector:@selector(compareName:)];
        NSLog(@"按照名字排序:%@",studentArray3);
        
        //按照先年龄再名字排序
        NSArray *studentArray4 = [studentArray1 sortedArrayUsingSelector:@selector(compareAgeAndName:)];
        NSLog(@"按照先年龄再名字排序:%@",studentArray4);

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Objective-C 对象,实例和方法发布时间:2022-07-12
下一篇:
Objective-C中的@property发布时间:2022-07-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap