Foundation框架(7)——NSArray的排序(普通排序、使用block排序、高级排序)

//
//  main.m
//  Foundation框架(7)-数组排序
//
//  Created by XinYou on 15-1-20.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
    
    
     
     
#import "Student.h"
#import "Book.h"

#pragma mark 数组排序1
void arraySort1(){

    NSArray *array = [NSArray arrayWithObjects:@"3", @"1", @"2", @"5", nil];
    
    // 排序之后会返回一个新的数组,而原数组array不变
    // 这里指定了比较的方法compare:,为什么是compare:而不是其他方法呢?
    // 因为array中的元素都是字符串NSString,而NSString实现了compare:方法
    NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];
    
    NSLog(@"array2 = %@", array2);

}

#pragma mark 数组排序2
void arraySort2(){

    Student *stu1 = [Student studentWithFirstname:@"DeHua" lastname:@"Liu"];
    Student *stu2 = [Student studentWithFirstname:@"XueYou" lastname:@"Zhang"];
    Student *stu3 = [Student studentWithFirstname:@"QingYun" lastname:@"Liu"];
    Student *stu4 = [Student studentWithFirstname:@"GuoRong" lastname:@"Zhang"];
    
    NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, stu4, nil];
    // 指定数组排序的方法为compareStudent:,因为Student中有一个compareStudent方法,而且该方法的返回值为NSComparisonResult
    NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)];
    
    NSLog(@"array2 = %@", array2);
}

#pragma mark 数组排序3
void arraySort3(){

    Student *stu1 = [Student studentWithFirstname:@"DeHua" lastname:@"Liu"];
    Student *stu2 = [Student studentWithFirstname:@"XueYou" lastname:@"Zhang"];
    Student *stu3 = [Student studentWithFirstname:@"QingYun" lastname:@"Liu"];
    Student *stu4 = [Student studentWithFirstname:@"GuoRong" lastname:@"Zhang"];
    
    NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, stu4, nil];
    // 利用block进行排序
    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 数组排序4-高级排序
void arraySort4(){

    Student *stu1 = [Student studentWithFirstname:@"DeHua" lastname:@"Liu" bookName:@"ios开发"];
    Student *stu2 = [Student studentWithFirstname:@"XueYou" lastname:@"Zhang" bookName:@"ios开发"];
    Student *stu3 = [Student studentWithFirstname:@"QingYun" lastname:@"Liu" bookName:@"android开发"];
    Student *stu4 = [Student studentWithFirstname:@"GuoRong" lastname:@"Zhang" bookName:@"android开发"];

    NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, stu4, nil];
    
    // 1,先按照书名进行排序,注意:这里的key写的是@property的名称,YES表示升序,NO表示降序
    NSSortDescriptor *booNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.bookName" ascending:YES];
    // 2,然后按照姓氏进行排序
    NSSortDescriptor *lastnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];
    // 3,最后按照名字进行排序
    NSSortDescriptor *firstnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
    
    // 按顺序添加排序描述器
    NSArray *descArray = [NSArray arrayWithObjects:booNameDesc, lastnameDesc, firstnameDesc, nil];
    
    NSArray *array2 = [array sortedArrayUsingDescriptors:descArray];
    
    NSLog(@"array2 = %@", array2);
}

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

    @autoreleasepool {
        
//        arraySort1();
//        arraySort2();
//        arraySort3();
        arraySort4();
        
    }
    return 0;
}
//
//  Student.h
//  Foundation框架(7)-数组排序
//
//  Created by XinYou on 15-1-20.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
     
     
      
      
@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
- (NSComparisonResult)compareStudent:(Student *)stu;

@end
//
//  Student.m
//  Foundation框架(7)-数组排序
//
//  Created by XinYou on 15-1-20.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#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 alloc] init] autorelease];
//    
//    stu.firstname = firstname;
//    stu.lastname = lastname;
    
    Student *stu = [Student studentWithFirstname:firstname lastname:lastname];
    stu.book = [Book bookWithName:bookName];
    
    return stu;
}

- (NSComparisonResult)compareStudent:(Student *)stu{
    // 先按照姓氏排序
    NSComparisonResult result = [self.lastname compare:stu.lastname];
    // 如果姓氏相同,再按照名字排序
    if (result == NSOrderedSame) {
        result = [self.firstname compare:stu.firstname];
    }
    
    return result;
}

- (NSString *)description{


    return [NSString stringWithFormat:@"[%@ %@-%@", self.lastname, self.firstname, self.book.bookName];
}

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

@end
//
//  Book.h
//  Foundation框架(7)-数组排序
//
//  Created by XinYou on 15-1-20.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
      
      
       
       

@interface Book : NSObject

@property (nonatomic, retain) NSString *bookName;

+ (id)bookWithName:(NSString *)bookName;

@end
//
//  Book.m
//  Foundation框架(7)-数组排序
//
//  Created by XinYou on 15-1-20.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "Book.h"

@implementation Book

+ (id)bookWithName:(NSString *)bookName{

    Book *book = [[[Book alloc] init] autorelease];
    book.bookName = bookName;
    
    return book;

}

- (void)dealloc{
    [_bookName release];

    [super dealloc];
}

@end

      
      
     
     
    
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值