Foundation NSArray的排序

来源:互联网 发布:淘宝网店价值评估 编辑:程序博客网 时间:2024/05/22 05:02


////  Book.h//  NSArray的排序////  Created by LiuWei on 15/4/18.//  Copyright (c) 2015年 LiuWei. All rights reserved.//#import <Foundation/Foundation.h>@interface Book : NSObject@property (nonatomic, strong)NSString* bookname;+ (id)bookWithName:(NSString*)bookname;@end

////  Book.m//  NSArray的排序////  Created by LiuWei on 15/4/18.//  Copyright (c) 2015年 LiuWei. All rights reserved.//#import "Book.h"@implementation Book+ (id)bookWithName:(NSString *)bookname{    Book *b = [[Book alloc]init];    b.bookname = bookname;        return b;}@end

////  Student.h//  NSArray的排序////  Created by LiuWei on 15/4/18.//  Copyright (c) 2015年 LiuWei. All rights reserved.//#import <Foundation/Foundation.h>@class Book;@interface Student : NSObject@property (nonatomic, strong)NSString *firstname; // 名@property (nonatomic, strong)NSString *lastname; // 姓@property (nonatomic, strong)Book *book;+ (id)studentWithFirstname:(NSString*)firstname lastname:(NSString*)lastname;+ (id)studentWithFirstname:(NSString *)firstname lastname:(NSString *)lastname bookname:(NSString*)bookname;- (NSComparisonResult)compareName:(Student*)stu;@end

////  Student.m//  NSArray的排序////  Created by LiuWei on 15/4/18.//  Copyright (c) 2015年 LiuWei. All rights reserved.//#import "Student.h"#import "Book.h"@implementation Student+ (id)studentWithFirstname:(NSString *)firstname lastname:(NSString *)lastname{    Student *stu = [[Student alloc]init];    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)compareName:(Student *)stu{    // 先比较姓    NSComparisonResult result = [self.lastname compare:stu.lastname];    if (result == NSOrderedSame)    {        // 如果姓相同, 则再比较名        result = [self.firstname compare:stu.firstname];    }        return result;}- (void)dealloc{    NSLog(@"%@  dealloc", self);}- (NSString *)description{    return [NSString stringWithFormat:@"%@ %@ %@", _lastname, _firstname, _book.bookname];}@end

////  main.m//  NSArray的排序////  Created by LiuWei on 15/4/18.//  Copyright (c) 2015年 LiuWei. All rights reserved.//#import <Foundation/Foundation.h>#import "Student.h"// sortedArrayUsingSelector: 排序void sort1();// sortedArrayUsingComparator:void sort2();// sortedArrayUsingDescriptors:void sort3();int main(){    @autoreleasepool    {        // sort1();        // sort2();        sort3();            }            return 0;}#pragma mark - sortedArrayUsingSelector:void sort1(){    @autoreleasepool    {        Student *stu1 = [Student studentWithFirstname:@"Wei" lastname:@"Liu"];        Student *stu2 = [Student studentWithFirstname:@"LiDan" lastname:@"Jiang"];        Student *stu3 = [Student studentWithFirstname:@"GuoMing" lastname:@"Cai"];        Student *stu4 = [Student studentWithFirstname:@"XiaoFeng" lastname:@"Liu"];                NSArray *arr = [NSArray arrayWithObjects:stu1, stu2, stu3, stu4, nil];                // SEL 返回值为类型为 NSComparisonResult        // sotedArrayUsingSelector: 会根据SEL 生成排序后的新数组        NSArray *arr2 = [arr sortedArrayUsingSelector:@selector(compareName:)];        NSLog(@"%@", arr2);    }}#pragma mark - sortedArrayUsingComparator:void sort2(){    @autoreleasepool    {        Student *stu1 = [Student studentWithFirstname:@"Wei" lastname:@"Liu"];        Student *stu2 = [Student studentWithFirstname:@"LiDan" lastname:@"Jiang"];        Student *stu3 = [Student studentWithFirstname:@"GuoMing" lastname:@"Cai"];        Student *stu4 = [Student studentWithFirstname:@"XiaoFeng" lastname:@"Liu"];                NSArray *arr = [NSArray arrayWithObjects:stu1, stu2, stu3, stu4, nil];                NSArray *sortedArray = [arr sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2)         {             NSComparisonResult result = [obj1 compareName:obj2];             return result;         }];                NSLog(@"%@", sortedArray);    }}#pragma mark - sortedArrayUsingDescriptors:void sort3(){    Student *stu1 = [Student studentWithFirstname:@"Wei" lastname:@"Liu" bookname:@"book4"];    Student *stu2 = [Student studentWithFirstname:@"LiDan" lastname:@"Jiang" bookname:@"book1"];    Student *stu3 = [Student studentWithFirstname:@"GuoMing" lastname:@"Cai" bookname:@"book3"];    Student *stu4 = [Student studentWithFirstname:@"XiaoFeng" lastname:@"Liu" bookname:@"book1"];        // 先按照书名排序    NSSortDescriptor *booknameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.bookname" ascending:YES];    // 再按照姓排序    NSSortDescriptor *lastnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];        // 再按照名排序    NSSortDescriptor *firstnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];        NSArray *array = @[stu1, stu2, stu3, stu4];        // 按顺序添加排序描述器    NSArray *desc = @[booknameDesc, lastnameDesc, firstnameDesc];    // 根据描述器的顺序生成排序好的数组    NSArray *sortedArray = [array sortedArrayUsingDescriptors:desc];    NSLog(@"%@", sortedArray);}


0 0
原创粉丝点击