iOS数组排序
2016-07-02 08:55
387 查看
一、利用NSSortDescriptor对象数组排序
NSSortDescriptor可以根据数组中对象的属性来排序,为排序数组的要排序的属性创建NSSortDescriptor对象,将所有这些对象放入一个数组中,该数组将会在后面用作参数。使用NSArray类的sortedArrayUsingDescripors:方法并将NSSortDescriptor对象数组作为参数传递过去,会返回一个排好序的数组。
创建Person类用于排序
Person.h
Person.m
排序
二、利用sortedArrayUsingComparator
调用NSComparator
makeObjectsPerformSelector:@select(aMethod) 让数组中的每个元素都调用aMethod
makeObjectsPerformSelector:@select(aMethod)withObject:oneObject 让数组中的每个元素 都调用 aMethod 并把 withObject 后边的 oneObject 对象做为参数传给方法aMethod ↩
NSSortDescriptor可以根据数组中对象的属性来排序,为排序数组的要排序的属性创建NSSortDescriptor对象,将所有这些对象放入一个数组中,该数组将会在后面用作参数。使用NSArray类的sortedArrayUsingDescripors:方法并将NSSortDescriptor对象数组作为参数传递过去,会返回一个排好序的数组。
创建Person类用于排序
Person.h
@interface Person : NSObject @property (nonatomic, copy) NSString *firstName; @property (nonatomic, copy) NSString *lastName; @property (nonatomic, assign) NSInteger age; -(instancetype)initWithFirstName:(NSString *)fName lastName:(NSString *)lName age:(NSInteger) age;
Person.m
@implementation Person -(instancetype)initWithFirstName:(NSString *)fName lastName:(NSString *)lName age:(NSInteger)age { self = [super init]; if (self) { self.firstName = fName; self.lastName = lName; self.age = age; } return self; }
排序
Person *p1 = [[Person alloc] initWithFirstName:@"Wenxuan" lastName:@"Huo" age:21]; Person *p2 = [[Person alloc] initWithFirstName:@"MaHa" lastName:@"B" age:30]; Person *p3 = [[Person alloc] initWithFirstName:@"MeKe" lastName:@"C" age:30]; Person *p4 = [[Person alloc] initWithFirstName:@"MaLian" lastName:@"A" age:30]; Person *p5 = [[Person alloc] initWithFirstName:@"HoHo" lastName:@"A" age:40]; Person *p6 = [[Person alloc] initWithFirstName:@"Guo" lastName:@"Zhong" age:5000]; // 包含所有Person的数组 NSArray *peopleArray = @[p1, p2, p3, p4, p5, p6]; // 为每个要排序的属性创建NSSortDescriptor对象 NSSortDescriptor * sdFirstName = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES]; NSSortDescriptor * sdLastName = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES]; NSSortDescriptor * sdAge = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES]; // 设置排序优先级,并组成数组。这里优先级最高为age,之后是lastName,最后是firstName NSArray * sortedArray = [peopleArray sortedArrayUsingDescriptors:@[sdAge, sdLastName, sdFirstName]]; // 为数组中每个元素执行方法,输出状态 [sortedArray makeObjectsPerformSelector:@selector(printState)];
makeObjectsPerformSelector1
二、利用sortedArrayUsingComparator
调用NSComparator
//升序排列 NSComparator cmptr = ^(id obj1,id obj2){ if([obj1 integerValue] > [obj2 integerValue]) { return (NSComparisonResult)NSOrderedDescending; } if([obj1 integerValue] < [obj2 integerValue]) { return (NSComparisonResult)NSOrderedAscending; } return (NSComparisonResult)NSOrderedSame; }; NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil]; sortArray = [sortArray sortedArrayUsingComparator:cmptr];
makeObjectsPerformSelector:@select(aMethod) 让数组中的每个元素都调用aMethod
makeObjectsPerformSelector:@select(aMethod)withObject:oneObject 让数组中的每个元素 都调用 aMethod 并把 withObject 后边的 oneObject 对象做为参数传给方法aMethod ↩
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法