Objective C中数组排序的三种方法

参考http://my.oschina.net/u/554222/blog/173958

文件.h

#import <Foundation/Foundation.h>

#import "AddressCard.h"


@interface AddressBook : NSMutableArray<NSCoding>

@property(retain) NSMutableArray *book;

-(void)seek:(AddressCard *)addressCard;

-(NSMutableArray *)sort;

-(void)display;

-(NSComparisonResult)compare:(AddressCard *)card1 card2:(AddressCard *)card2;

//-(NSComparisonResult)compare:(AddressCard *)card;

@end

文件.m

#import "AddressBook.h"


@implementation AddressBook

-(void)seek:(AddressCard *)addressCard

{

    for (NSString *seekName in self.book)

    {

        if ([addressCard.name isEqualToString:seekName])

        {

            NSLog(@"%@",addressCard);

            break;

        }

    }

    NSLog(@"Have no the person");

}

//简单排序

//-(NSMutableArray *)sort

//{

//    NSArray *sortedarray=[self.book sortedArrayUsingSelector:@selector(compare:)];

//    if ([self.book sortedArrayUsingSelector:@selector(compare:)]==nil)

//    {

//        NSLog(@"操作失败");

//    }

//    else

//    {

//        NSLog(@"操作成功");

//    }

    sortedarray=[NSArray arrayWithObjects:@"abc",@"sfd", nil];

//    NSLog(@"%@",sortedarray);

//    NSLog(@"sortedarray:%p  book:%p",sortedarray,self.book);

    [self.book removeAllObjects];

    self.book=[[NSMutableArray alloc] init];

//    self.book=[NSMutableArray arrayWithArray:sortedarray];

//    return self;

//}

//block语法排序

//-(NSMutableArray *)sort

//{

//    NSArray *sortedarray=[self.book sortedArrayUsingComparator:^NSComparisonResult(AddressCard *card1, AddressCard *card2)

//    {

//        NSComparisonResult result=[card1.name compare:card2.name];

//        return result;

//    }];

//    self.book=[NSMutableArray arrayWithArray:sortedarray];

//    return self;

//}

//高级排序,,

-(NSMutableArray *)sort

{

    NSSortDescriptor *nameDesc=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];

    NSSortDescriptor *emailDesc=[NSSortDescriptor sortDescriptorWithKey:@"email" ascending:YES];

    NSSortDescriptor *phoneDesc=[NSSortDescriptor sortDescriptorWithKey:@"phoneNumber" ascending:YES];

    

    NSArray *descArray=[NSArray arrayWithObjects:nameDesc,emailDesc,phoneDesc, nil];

    NSArray *sortedArray=[self.book sortedArrayUsingDescriptors:descArray];

    self.book=[NSMutableArray arrayWithArray:sortedArray];

    return self;

}

-(void)display

{

    for (AddressCard *str in self.book)

    {

        NSLog(@"%@",str);

    }

}

-(NSComparisonResult)compare:(AddressCard *)card1 card2:(AddressCard *)card2

{

    NSComparisonResult result=[card1.name compare:card2.name];

    return result;

}

//-(NSComparisonResult)compare:(AddressCard *)card

//{

//    if ([self.book indexOfObject:card]==0)

//    {

//        return NSOrderedSame;

//    }

//    else

//    {

//        NSComparisonResult result=[[[self.book objectAtIndex:([self.book indexOfObject:card]+1)] name] compare:card.name];

//        return result;

//    }

//}

-(void)encodeWithCoder:(NSCoder *)aCoder

{

    [aCoder encodeObject:self.book forKey:@"book"];

}

-(id)initWithCoder:(NSCoder *)aDecoder

{

    if (self=[super init])

    {

        self.book=[aDecoder decodeObjectForKey:@"book"];

    }

    return self;

}


@end

高级排序使用过程中注意对象的使用是否正确,应特别注意

NSSortDescriptor *nameDesc=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];

中的Key,假如数组self.book中元素有自定义类,如:若name为自定类,类中有firstName,则Key应为@"name.firstName",具体如:

NSSortDescriptor *nameDesc=[NSSortDescriptor sortDescriptorWithKey:@"name.firstName" ascending:YES];

转载于:https://my.oschina.net/u/1432769/blog/189754

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值