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
- Foundation NSArray的排序
- Foundation框架(7)——NSArray的排序(普通排序、使用block排序、高级排序)
- 【Foundation-10-3】#import <Foundation/NSArray.h>排序
- Foundation--NSArray
- Foundation NSArray
- Foundation-NSArray
- Objective-C之foundation中四种数组NSArray的排序方法
- Cocoa Foundation 框架:NSArray数组的使用
- IOS学习--Foundation--NSArray的使用
- Foundation框架--NSArray 的介绍和使用
- Foundation框架–NSArray 的遍历方法
- foundation的使用——NSArray
- NSArray 的排序
- NSArray的排序方法
- nsarray的排序
- NSArray的排序方法
- ios的NSArray排序
- NSArray的排序
- hdu 1016 Prime Ring Problem
- java实现String的split方法
- Java实现排序二叉树的操作
- POJ 2528-Mayor's posters(线段树区间染色+离散化)
- 浅析设计模式之适配器模式
- Foundation NSArray的排序
- 自己动手写C语言库函数(3) strstr
- 链表的实现
- RSA加密解密原理
- poj1696Space Ant
- UVA 10361 解题报告
- HDU 5207 Greatest Greatest Common Divisor
- 图书管理系统的操作员信息管理界面及其功能设计
- 【面试题】判断字符串是否匹配一定的模式