标签:sort nsmutablearray nsarray
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;
#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
#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"
)
标签:sort nsmutablearray nsarray
原文地址:http://blog.csdn.net/duoduo_333/article/details/38563987