码迷,mamicode.com
首页 > 编程语言 > 详细

NSArary自定义对象排序 NSComparator, compare

时间:2015-08-07 01:49:32      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

reference from :http://mobile.51cto.com/hot-434804.htm

1.构建Person类

Person.h

@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *surname;
@property (nonatomic, strong) NSDate *dateOfBirth;
@end

 

2. 初始化ObjSort对象排序功能,包含一个NSArray数组,数组元素为自定义Person

ObjSort.h

/**
 * 对NSArray自定义对象排序类
 */
@interface ObjSort : NSObject
-(void)personsSort_NSComparator;//NSComparator比较器
@end

 

ObjSort.m

@interface ObjSort()
@property (nonatomic, strong) NSMutableArray *personObjs;//存储了若干个Person类对象的数组
@end


@implementation ObjSort

/**
 * 初始化对象
 * 初始化NSMutableArray数组personObjs的初始内容, 包装Person类对象
 */
-(instancetype)init{
    self = [super init];
    if (self == nil) {
        return nil;
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd/MM/yyyy"];
    
    Person *person[5];
    for(int i=0; i<5; i++){
        person[i] = [[Person alloc] init];
    }
    
    person[0].name = @"Smith";
    person[0].surname = @"John";
    person[0].dateOfBirth = [formatter dateFromString:@"03/01/1984"];
    
    person[1].name = @"Andersen";
    person[1].surname = @"Jane";
    person[1].dateOfBirth = [formatter dateFromString:@"16/03/1979"];
    
    person[2].name = @"Clark";
    person[2].surname = @"Anne";
    person[2].dateOfBirth = [formatter dateFromString:@"13/09/1995"];
    
    person[3].name = @"Smith";
    person[3].surname = @"David";
    person[3].dateOfBirth = [formatter dateFromString:@"19/07/1981"];
    
    person[4].name = @"Johnson";
    person[4].surname = @"Rose";
    person[4].dateOfBirth = [formatter dateFromString:@"22/02/1989"];

    if (_personObjs==nil) {
        _personObjs = [[NSMutableArray alloc] initWithCapacity:5];
    }
    for (int i=0; i<5; i++) {
        [_personObjs addObject:person[i]];
    }
    
    [self showPersons:_personObjs];
    return self;
}

/**
 * 日志打印输出当前NSArray内容
 */
-(void)showPersons:(NSArray *)array{
    Person *person;
    
    if (_personObjs == nil) {
        NSLog(@"nil");
    }
    
    for (int i =0; i< [array count]; i++) {
        person = [array objectAtIndex:i];
        if ([person isKindOfClass:[Person class]]) {
            NSLog(@"---Person %d---", i);
            NSLog(@"Name: %@", person.name);
            NSLog(@"Surname: %@", person.surname);
            NSLog(@"DateOfBirth: %@", person.dateOfBirth);
        }
        else{
            NSLog(@"Object in personObjs is not Person class object.");
        }
    }
  NSLog(@"All person print over.\n\n"); }

 

方法一:NSComparator block + NSArray的 sortedArrayUsingSelector

在ObjSort中实现方法personsSort_NSComparator

/**
 * 采用NSComparator比较的排序方法
 */
-(void)personsSort_NSComparator{
    
    NSComparator sortBlock = ^(Person *p1 , Person *p2){
        NSComparisonResult r1 = [p1.name compare:p2.name];
        if(r1 != NSOrderedSame){
            return r1;
        }else {
            NSComparisonResult r2 = [p1.surname compare:p2.surname];
            if (r2 != NSOrderedSame)
                return r2;
            else
                return [p1.dateOfBirth compare:p2.dateOfBirth];
        }
    };
    NSArray *personList = [self.personObjs sortedArrayUsingComparator:sortBlock];
    NSArray *personList = [self.personObjs sortedArrayUsingSelector:@selector(compare:)];
    [self showPersons:personList];
}

 

方法二:利用对象的自定义方法compare:

Person.m中添加

-(NSComparisonResult)compare:(Person *)otherPerson{
    NSComparisonResult r1 = [self.name compare:otherPerson.name];
    if(r1 != NSOrderedSame){
        return r1;
    }else {
        NSComparisonResult r2 = [self.surname compare:otherPerson.surname];
        if (r2 != NSOrderedSame)
            return r2;
        else
            return [self.dateOfBirth compare:otherPerson.dateOfBirth];
    }
    return [self.name compare:otherPerson.name];
}

ObjSort.m中添加

/**
 * 采用自定义对象自定义函数compare进行NSArray排序
 */
-(void)personsSort_CustomCompare{
    NSArray *personList = [self.personObjs sortedArrayUsingSelector:@selector(compare:)];
    [self showPersons:personList];
}

 在ObjSort.h中添加响应的接口

/**
 * 对NSArray自定义对象排序类
 */
@interface ObjSort : NSObject
-(void)personsSort_NSComparator;//NSComparator比较器
-(void)personsSort_CustomCompare;//对象自定义比较方法Compare
@end

 

最后,在main.m(排序调用处)添加

        ObjSort *objSort = [[ObjSort alloc] init];
        [objSort personsSort_NSComparator];

 

方法一结果

---Person 0---
Name: Smith
Surname: John
DateOfBirth: 1984-01-02 16:00:00 +0000
---Person 1---
Name: Andersen
Surname: Jane
DateOfBirth: 1979-03-15 16:00:00 +0000
---Person 2---
Name: Clark
Surname: Anne
DateOfBirth: 1995-09-12 16:00:00 +0000
---Person 3---
Name: Smith
Surname: David
DateOfBirth: 1981-07-18 16:00:00 +0000
---Person 4---
Name: Johnson
Surname: Rose
DateOfBirth: 1989-02-21 16:00:00 +0000
All person print over.


---Person 0---
Name: Andersen
Surname: Jane
DateOfBirth: 1979-03-15 16:00:00 +0000
---Person 1---
Name: Clark
Surname: Anne
DateOfBirth: 1995-09-12 16:00:00 +0000
---Person 2---
Name: Johnson
Surname: Rose
DateOfBirth: 1989-02-21 16:00:00 +0000
---Person 3---
Name: Smith
Surname: David
DateOfBirth: 1981-07-18 16:00:00 +0000
---Person 4---
Name: Smith
Surname: John
DateOfBirth: 1984-01-02 16:00:00 +0000
All person print over.

 

方法二结果

---Person 0---
Name: Smith
Surname: John
DateOfBirth: 1984-01-02 16:00:00 +0000
---Person 1---
Name: Andersen
Surname: Jane
DateOfBirth: 1979-03-15 16:00:00 +0000
---Person 2---
Name: Clark
Surname: Anne
DateOfBirth: 1995-09-12 16:00:00 +0000
---Person 3---
Name: Smith
Surname: David
DateOfBirth: 1981-07-18 16:00:00 +0000
---Person 4---
Name: Johnson
Surname: Rose
DateOfBirth: 1989-02-21 16:00:00 +0000
All person print over.


---Person 0---
Name: Andersen
Surname: Jane
DateOfBirth: 1979-03-15 16:00:00 +0000
---Person 1---
Name: Clark
Surname: Anne
DateOfBirth: 1995-09-12 16:00:00 +0000
---Person 2---
Name: Johnson
Surname: Rose
DateOfBirth: 1989-02-21 16:00:00 +0000
---Person 3---
Name: Smith
Surname: David
DateOfBirth: 1981-07-18 16:00:00 +0000
---Person 4---
Name: Smith
Surname: John
DateOfBirth: 1984-01-02 16:00:00 +0000
All person print over.

 

NSArary自定义对象排序 NSComparator, compare

标签:

原文地址:http://www.cnblogs.com/fortunely/p/4709598.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!