Objective C中数组排序的三种方法
Objective C中数组排序的三种方法
HillYoung 发表于4年前
Objective C中数组排序的三种方法
  • 发表于 4年前
  • 阅读 358
  • 收藏 4
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: 简单排序,使用block排序,高级排序

参考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];

共有 人打赏支持
粉丝 13
博文 87
码字总数 25501
×
HillYoung
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: