// // 创建学生类 姓名 学号 年龄 成绩
// 创建五个学生对象,加入数组, 分别按姓名升序 学号降序 年龄升序 成绩降序排序
main.m
1 #import <Foundation/Foundation.h> 2 #import "Student.h" 3 int main(int argc, const char * argv[]) { 4 @autoreleasepool { 5 // arc4random()%10 0---9 6 // 7 Student * s1 = [[Student alloc] initWithName:@"Jack" stuId:arc4random()%10+1 age:arc4random()%10+1 andScore:arc4random()%41 + 60]; 8 // 60 - 100 9 10 Student * s2 = [[Student alloc] initWithName:@"Tom" stuId:arc4random()%10+1 age:arc4random()%10+1 andScore:arc4random()%41 + 60]; 11 12 Student * s3 = [[Student alloc] initWithName:@"Lily" stuId:arc4random()%10+1 age:arc4random()%10+1 andScore:arc4random()%41 + 60]; 13 14 Student * s4 = [[Student alloc] initWithName:@"Lucy" stuId:arc4random()%10+1 age:arc4random()%10+1 andScore:arc4random()%41 + 60]; 15 Student * s5 = [[Student alloc] initWithName:@"Mark" stuId:arc4random()%10+1 age:arc4random()%10+1 andScore:arc4random()%41 + 60]; 16 17 // NSArray * arr = @[s1,s2,s3,s4,s5]; 18 NSMutableArray * arr = [NSMutableArray array]; 19 [arr addObject:s1]; 20 [arr addObject:s2]; 21 [arr addObject:s3]; 22 [arr addObject:s4]; 23 [arr addObject:s5]; 24 // 成绩 降序 25 for (int i = 0; i< [arr count]; i++) { 26 for (int j = 0; j < [arr count] - i -1; j++) { 27 Student * s1 = arr[j]; 28 Student * s2 = arr[j+1]; 29 if ([s1 isStuScoreLessThanAnother:s2]) { 30 [arr exchangeObjectAtIndex:j withObjectAtIndex:j+1]; 31 } 32 } 33 } 34 NSLog(@"%@",arr); 35 36 NSArray * arr1 = [arr sortedArrayUsingSelector:@selector(isStuScoreLessThanAnother:)]; 37 NSLog(@"%@",arr1); 38 39 // 年龄 左 < 右 40 41 NSArray * arr2 = [arr sortedArrayUsingComparator:^NSComparisonResult(Student * s1, Student * s2) { 42 return [s1 age] > [s2 age]; 43 }]; 44 45 NSLog(@"%@",arr2); 46 47 48 NSArray * arr3 = [arr sortedArrayUsingComparator:^NSComparisonResult(Student * obj1, Student * obj2) { 49 return [obj1 age] > [obj2 age]; 50 }]; 51 NSLog(@"%@",arr3); 52 53 NSArray * arr4 = [arr sortedArrayUsingComparator:^NSComparisonResult(Student * obj1, Student * obj2) { 54 return [obj1 score] < [obj2 score]; 55 }]; 56 NSLog(@"%@",arr4); 57 58 59 } 60 return 0; 61 }
1 #import <Foundation/Foundation.h> 2 3 @interface Student : NSObject { 4 NSString * _name; 5 int _stuId; 6 int _age; 7 int _score; 8 } 9 - (id)initWithName:(NSString *)name stuId:(int)stuId age:(int)age andScore:(int)score; 10 11 - (int)score; 12 - (int)age; 13 - (BOOL)isStuScoreLessThanAnother:(Student *)s2; 14 @end
1 #import "Student.h" 2 3 @implementation Student 4 - (id)initWithName:(NSString *)name stuId:(int)stuId age:(int)age andScore:(int)score { 5 if (self = [super init]) { 6 _name = name; 7 _age = age; 8 _stuId = stuId; 9 _score = score; 10 } 11 return self; 12 } 13 14 - (int)score { 15 return _score; 16 } 17 18 - (NSString *)description { 19 return [NSString stringWithFormat:@"name:%@ stuId:%d age:%d score:%d",_name,_stuId,_age,_score]; 20 } 21 22 - (int)age { 23 return _age; 24 } 25 26 - (BOOL)isStuScoreLessThanAnother:(Student *)s2 { 27 return [self score] < [s2 score]; 28 } 29 30 31 @end