在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
在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);
|
请发表评论