参考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;
文件.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;
}
高级排序使用过程中注意对象的使用是否正确,应特别注意
NSSortDescriptor *nameDesc=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
中的Key,假如数组self.book中元素有自定义类,如:若name为自定类,类中有firstName,则Key应为@"name.firstName",具体如:
NSSortDescriptor *nameDesc=[NSSortDescriptor sortDescriptorWithKey:@"name.firstName" ascending:YES];