NSArray 数组排序

  1. //方法1,使用自带的比较器

  2. //compare是数组自带的比较方法

  3. NSArray *array=[NSArray arrayWithObjects:@"3",@"1",@"2", nil];

  4. NSArray *array2= [array sortedArrayUsingSelector:@selector(compare:)];

  5. NSLog(@"%@",array2);

结果是升序排列

  1. //方式二:使用块完成排

  2. NSArray *array = [NSArray arrayWithObjects:@"1bc",@"4b6",@"123",@"789",@"3ef", nil];

  3. NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

  4.            

  5. //这里的代码可以参照上面compare:默认的排序方法,也可以把自定义的方法写在这里,给对象排序

  6.    NSComparisonResult result = [obj1 compare:obj2];

  7.    return result;

  8. }];

  9. NSLog(@"排序后:%@",sortedArray);

方法3:自定义排序

  1. #import <Foundation/Foundation.h>

  2.  

  3. @interface Person : NSObject

  4. @property NSString* name;

  5. @property int age;

  6.  

  7. -(id)initWithNameAndAge:(NSString*) aName and:(int) aAge;

  8. -(NSComparisonResult)comparePersonByAge:(Person *)person;

  9. -(NSComparisonResult)comparePersonByName:(Person *)person;

  10. @end

  1. #import "Person.h"

  2.  

  3. @implementation Person

  4. @synthesize name,age;

  5. -(id)initWithNameAndAge:(NSString*) aName and:(int) aAge{

  6.    if (self=[super init]) {

  7.        

  8.        name=aName;

  9.        age=aAge;

  10.        

  11.    }

  12.    return self;

  13. }

  14.  

  15. //自定义排序方法

  16. -(NSComparisonResult)comparePersonByAge:(Person *)person{

  17.    //默认按年龄排序

  18.    NSComparisonResult result = [[NSNumber numberWithInt:person.age] compare:[NSNumber numberWithInt:self.age]];//注意:基本数据类型要进行数据转换

  19.    //如果年龄一样,就按照名字排序

  20.    //if (result == NSOrderedSame) {

  21.    //    result = [self.name compare:person.name];

  22.    //}

  23.    return result;

  24. }

  25.  

  26. -(NSComparisonResult)comparePersonByName:(Person *)person{

  27.    //默认按年龄排序

  28.    NSComparisonResult result = [ person.name compare:self.name];//注意:基本数据类型要进行数据转换

  29.    //如果年龄一样,就按照名字排序

  30.    if (result == NSOrderedSame) {

  31.        result = [[NSNumber numberWithInt:person.age] compare:[NSNumber numberWithInt:self.age]];

  32.    }

  33.    return result;

  34. }

  35.  

  36.  

  37. - (NSString *)description

  38. {

  39.    return [NSString stringWithFormat:@"%@    %d", name,age];

  40. }

  41. @end

  1. #import <Foundation/Foundation.h>

  2. #import "Person.h"

  3.  

  4. int main(int argc, const char * argv[]) {

  5.    @autoreleasepool {

  6.        Person *p1 = [[Person alloc]initWithNameAndAge:@" qweasadsasd" and:25];

  7.        Person *p2 = [[Person alloc]initWithNameAndAge:@"\t1234" and:28];

  8.        Person *p3 = [[Person alloc]initWithNameAndAge:@"123" and:2];

  9.        Person *p4 = [[Person alloc]initWithNameAndAge:@"zxc" and:89];

  10.        Person *p5 = [[Person alloc]initWithNameAndAge:@"123" and:8];

  11.        

  12.        NSArray * persons = [NSArray arrayWithObjects:p1,p2,p3,p4,p5,nil];

  13.        NSArray *sortedArray = [persons sortedArrayUsingSelector:@selector(comparePersonByName:)];

  14.        NSLog(@"排序后:%@",sortedArray);

  15.    }

  16.    return 0;

  17. }

方法四:高级排序

posted @ 2015-09-18 15:42  Nicat  阅读(237)  评论(0编辑  收藏  举报