Sort方法的扩展
OC中类方法中仅仅为我们提供了一些降序方法,如今我们自定义方法,实现升序.
1.要求:定义一个Person类,实例变量包含name,age,height,定义几个对象,把这些对象保存在数组中,自定义方法,实现数组按name,age,gheight的升序排列输出.
首先Person.h文件
#import <Foundation/Foundation.h> @interface Person : NSObject{ NSString *_name;//姓名 NSInteger _age;//年龄 CGFloat _height;//身高 } //初始化 - (id)initWithName:(NSString *)name age:(NSInteger)age height:(CGFloat)height; //姓名 - (void)setName:(NSString *)name; - (NSString *)name; //年龄 - (void)setAge:(NSInteger)age; - (NSInteger)age; //身高 - (void)setHeight:(CGFloat)height; - (CGFloat)height; //比較age - (NSComparisonResult)compareByAge:(Person *)anPerson; //比較身高 - (NSComparisonResult)compareByHeight:(Person *)anPerson; //比較名字升序 - (NSComparisonResult)compareByName:(Person *)anPerson; //名字降序 - (NSComparisonResult)compareByNameDesceding:(Person *)anPerson;
Person.m文件
#import "Person.h" @implementation Person #pragma mark - setter and getter //set姓名 - (void)setName:(NSString *)name{ _name = name; } - (NSString *)name{ return _name; } //set年龄 - (void)setAge:(NSInteger)age{ _age = age; } - (NSInteger)age{ return _age; } //set身高 - (void)setHeight:(CGFloat)height{ _height = height; } - (CGFloat)height{ return _height; } #pragma mark - init method //初始化 - (id)initWithName:(NSString *)name age:(NSInteger)age height:(CGFloat)height{ self = [super init]; if (self != nil) { _name = name; _age = age; _height = height; } return self; } #pragma mark - override method //重写description - (NSString *)description{ return [NSString stringWithFormat:@"name:%@ age:%ld, height:%g", _name, _age, _height]; } #pragma mark - compare //比較年龄age - (NSComparisonResult)compareByAge:(Person *)anPerson{ if ([self age] > [anPerson age]) { return NSOrderedDescending;//1 降序 }else if([self age] == [anPerson age]){ //相等 return NSOrderedSame;//0 }else{ return NSOrderedAscending;//-1 升序 } } //比較身高 - (NSComparisonResult)compareByHeight:(Person *)anPerson{ if ([self height] > [anPerson height]) { return NSOrderedDescending; }else if([self height] == [anPerson height]){ return NSOrderedSame; }else{ return NSOrderedAscending; } } //比較名字 - (NSComparisonResult)compareByName:(Person *)anPerson{ return [[self name] compare:[anPerson name]]; } //名字降序 - (NSComparisonResult)compareByNameDesceding:(Person *)anPerson{ return -[[self name] compare:[anPerson name]]; } @end
主函数中main.m调用
#import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * argv[]) { @autoreleasepool { Person *per1 = [[Person alloc] initWithName:@"xiaoshuai" age:12 height:178]; Person *per2 = [[Person alloc] initWithName:@"xiaomeng" age:22 height:175]; Person *per3 = [[Person alloc] initWithName:@"xiaoguangguang" age:20 height:180]; Person *per4 = [[Person alloc] initWithName:@"dapeng" age:30 height:190]; NSArray *perArr = [[NSArray alloc] initWithObjects:per1,per2,per3,per4, nil]; //对数组按姓名升序排序 NSArray *sortArrName = [perArr sortedArrayUsingSelector:@selector(compareByName:)]; NSLog(@"%@", sortArrName); //对数组中年龄升序排序 NSArray *sortArrAge = [perArr sortedArrayUsingSelector:@selector(compareByAge:)]; NSLog(@"%@", sortArrAge); //对数组中身高按升序排序 NSArray *sortArrHeight = [perArr sortedArrayUsingSelector:@selector(compareByHeight:)]; NSLog(@"%@", sortArrHeight); //名字降序 NSArray *sortArrNameDescending = [perArr sortedArrayUsingSelector:@selector(compareByNameDesceding:)]; NSLog(@"%@", sortArrNameDescending); } return 0; }
測试结果:
2014-08-14 22:03:37.650 Lesson_Sort[4908:303] (
"name:dapeng age:30, height:190",
"name:xiaoguangguang age:20, height:180",
"name:xiaomeng age:22, height:175",
"name:xiaoshuai age:12, height:178"
)
2014-08-14 22:03:37.651 Lesson_Sort[4908:303] (
"name:xiaoshuai age:12, height:178",
"name:xiaoguangguang age:20, height:180",
"name:xiaomeng age:22, height:175",
"name:dapeng age:30, height:190"
)
2014-08-14 22:03:37.652 Lesson_Sort[4908:303] (
"name:xiaomeng age:22, height:175",
"name:xiaoshuai age:12, height:178",
"name:xiaoguangguang age:20, height:180",
"name:dapeng age:30, height:190"
)
2014-08-14 22:03:37.652 Lesson_Sort[4908:303] (
"name:xiaoshuai age:12, height:178",
"name:xiaomeng age:22, height:175",
"name:xiaoguangguang age:20, height:180",
"name:dapeng age:30, height:190"
)