Objective-C之foundation中四种数组NSArray的排序方法

demo代码如下:
数组排序有四种方法:
1、基本排序方式:
利用NSString中提供的compare方法进行排序,见demo的arraySort1
2、自定义compare
在实体类中自定义compareStudent方法,实现排序逻辑
3、利用block进行排序:
sortedArrayUsingComparator
4、高级排序方法
利用排序描述器,自定义排序优先级

Book.h文件:
#import <Foundation/Foundation.h>
@interface Book : NSObject

@property (nonatomic, retain) NSString * name;
+ (id) bookWithName:(NSString *) name;
@end

Book.m文件:
#import "Book.h"
#import "Book.h"

@implementation Book
+(id) bookWithName:(NSString *)name{
    Book *book = [[[Book alloc] init] autorelease];
    book.name = name;
    return book;
}

- (void)dealloc{
    [_name release];
    [super dealloc];
}
@end

Student.h文件:
#import <Foundation/Foundation.h>

@class Book;
@interface Student : NSObject

@property (nonatomic, retain) NSString *firstname;
@property (nonatomic, retain) NSString *lastname;
@property (nonatomic, retain) Book *book;

+ (id) studentWithFirstname:(NSString *)firstname  lastname:(NSString *) lastname;
+ (id) studentWithFirstname:(NSString *)firstname  lastname:(NSString *) lastname bookName:(NSString *) bookName;

- (NSComparisonResult)compareStudent:(Student *)stu;
@end

Student.m文件:

#import "Student.h"
#import "Book.h"
@implementation Student

+ (id)studentWithFirstname:(NSString *)firstname lastname:(NSString *)lastname{
    Student *stu = [[[Student alloc] init] autorelease];
    stu.firstname = firstname;
    stu.lastname = lastname;
    return stu;
}

+ (id)studentWithFirstname:(NSString *)firstname lastname:(NSString *)lastname bookName:(NSString *)bookName{
    Student *stu = [Student studentWithFirstname:firstname lastname:lastname];
    stu.book = [Book bookWithName:bookName];
     
    return stu;
}

//返回值类型必须是NSComparisonResult
- (NSComparisonResult) compareStudent:(Student *)stu{
   //姓进行排序
    NSComparisonResult result = [self.lastname compare: stu.lastname];
    //如果姓相同,则根据名排序
    if(result == NSOrderedSame){
        result = [self.firstname compare:stu.firstname];
    }
    return result;
}
//重写description方法
- (NSString *) description{
  return   [NSString stringWithFormat:@"%@ %@-%@",self.lastname, self.firstname,self.book.name];
}

- (void) dealloc{
    [_firstname release];
    [_lastname release];
    [_book release];
    [super dealloc];
}

@end

main测试文件:

#import <Foundation/Foundation.h>
#import "Student.h"

#pragma mark sort1
void arraySort1(){
    NSArray *array = [NSArray arrayWithObjects:@"1",@"5",@"2",@"8",@"3", nil];
    //指定元素的比较方法compare
    NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"array2:%@", array2);
}


void arraySort2(){

    Student *stu1=[Student studentWithFirstname:@"1" lastname:@"wang"];
    Student *stu2=[Student studentWithFirstname:@"1" lastname:@"li"];
    Student *stu3=[Student studentWithFirstname:@"2" lastname:@"wang"];
    Student *stu4=[Student studentWithFirstname:@"3" lastname:@"zhang"];

    NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
    NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)];
    NSLog(@"%@", array2);
}

void arraySort3(){
    Student *stu1=[Student studentWithFirstname:@"1" lastname:@"wang"];
    Student *stu2=[Student studentWithFirstname:@"1" lastname:@"li"];
    Student *stu3=[Student studentWithFirstname:@"2" lastname:@"wang"];
    Student *stu4=[Student studentWithFirstname:@"3" lastname:@"zhang"];
   
    NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];

    NSArray *array2 = [array sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {
        //姓进行排序
        NSComparisonResult result = [obj1.lastname compare: obj2.lastname];
        //如果姓相同,则根据名排序
        if(result == NSOrderedSame){
            result = [obj1.firstname compare:obj2.firstname];
        }
        return result;
    }];

    NSLog(@"array2:%@",array2);
}
#pragma mark 自定义优先级排序
void arraySort4(){
    Student *stu1=[Student studentWithFirstname:@"1" lastname:@"wang" bookName:@"book1"];
    Student *stu2=[Student studentWithFirstname:@"1" lastname:@"li" bookName:@"book3"];
    Student *stu3=[Student studentWithFirstname:@"2" lastname:@"wang" bookName:@"book1"];
    Student *stu4=[Student studentWithFirstname:@"3" lastname:@"zhang" bookName:@"book2"];
   
    NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
    //创建排序描述器 @"lastname" @property后面的名字一致
    NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
    NSSortDescriptor *lastnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];
    NSSortDescriptor *firstnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
    //按照需求要求的排序优先级排序
    NSArray *desc = [NSArray arrayWithObjects:bookNameDesc,lastnameDesc,firstnameDesc, nil];
    NSArray *array2 = [array sortedArrayUsingDescriptors:desc];
   
    NSLog(@"array2:%@",array2);
}


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

    @autoreleasepool {
       
        arraySort1();
        NSLog(@"------arraySort2------");
        arraySort2();
        NSLog(@"------arraySort3-------");
        arraySort3();
        NSLog(@"------arraySort4-------");
        arraySort4();
    }
    return 0;
}

执行打印结果:
2013-07-28 02:27:44.449 NSArray排序[3531:303] array2:(
    1,
    2,
    3,
    5,
    8
)
2013-07-28 02:27:44.473 NSArray排序[3531:303] ------arraySort2------
2013-07-28 02:27:44.475 NSArray排序[3531:303] (
    "li 1-(null)",
    "wang 1-(null)",
    "wang 2-(null)",
    "zhang 3-(null)"
)
2013-07-28 02:27:44.476 NSArray排序[3531:303] ------arraySort3-------
2013-07-28 02:27:44.478 NSArray排序[3531:303] array2:(
    "li 1-(null)",
    "wang 1-(null)",
    "wang 2-(null)",
    "zhang 3-(null)"
)
2013-07-28 02:27:44.478 NSArray排序[3531:303] ------arraySort4-------
2013-07-28 02:27:44.479 NSArray排序[3531:303] array2:(
    "wang 1-book1",
    "wang 2-book1",
    "zhang 3-book2",
    "li 1-book3"
)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值