ios数组里的model排序(高级排序)
来源:互联网 发布:罗浮威尔瓷砖 知乎 编辑:程序博客网 时间:2024/05/18 02:43
- (NSMutableArray *)datas{ if (!_datas) { _datas = [NSMutableArray array]; Person *p1 = [[Person alloc] initWithName:@"jack" age:20 score:97]; Person *p2 = [[Person alloc] initWithName:@"anne" age:8 score:33]; Person *p3 = [[Person alloc] initWithName:@"zhng" age:54 score:11]; Person *p4 = [[Person alloc] initWithName:@"tuoma" age:76 score:54]; Person *p5 = [[Person alloc] initWithName:@"gril" age:95 score:12]; Person *p6 = [[Person alloc] initWithName:@"boy" age:21 score:76]; Person *p7 = [[Person alloc] initWithName:@"big" age:53 score:98]; Person *p8 = [[Person alloc] initWithName:@"hack" age:33 score:66]; Person *p9 = [[Person alloc] initWithName:@"zoom" age:33 score:21]; Person *p10 = [[Person alloc] initWithName:@"right" age:69 score:88]; [_datas addObject:p1]; [_datas addObject:p2]; [_datas addObject:p3]; [_datas addObject:p4]; [_datas addObject:p5]; [_datas addObject:p6]; [_datas addObject:p7]; [_datas addObject:p8]; [_datas addObject:p9]; [_datas addObject:p10]; } return _datas;}
接下来我们需要进行排序:
规则如下:
1.首先按照年龄排序
2.如果年龄相同按照分数排序
那么我们需要创建排序描述者,一个描述着只能对一个属性进行描述 如果需要描述多个 我们需要创建多个描述者
我们这里的需求就需要创建两个描述者 一个是对年龄描述 一个是对分数描述 代码如下:
NSSortDescriptor *ageSD = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];//ascending:YES 代表升序 如果为NO 代表降序NSSortDescriptor *scoreSD=[NSSortDescriptor sortDescriptorWithKey:@"score" ascending:YES];创建好这两个描述者之后 我们就可以调用数组的 sortedArrayUsingDescriptors 方法来实现排序
sortedArrayUsingDescriptors方法接收一个数组的参数 里面放描述者 然后他会返回一个排序好的数组 所以我们这样做:
self.datas = [[self.datas sortedArrayUsingDescriptors:@[ageSD,scoreSD]] mutableCopy];我们点击导航栏左边的排序按钮的时候 会执行以下操作:
http://www.cnblogs.com/syios/p/5918868.html- (IBAction)sortAge:(id)sender { NSSortDescriptor *ageSD = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES]; NSSortDescriptor *scoreSD=[NSSortDescriptor sortDescriptorWithKey:@"score" ascending:YES]; self.datas = [[self.datas sortedArrayUsingDescriptors:@[ageSD,scoreSD]] mutableCopy];//[self.datas sortUsingDescriptors:@[ageSD,scoreSD]] 可能也可以[self.tableView reloadData]; }
0 0
- ios数组里的model排序(高级排序)
- ios 数组排序-高级排序
- iOS数组内存放自定义model的简单排序方法
- iOS数组根据key或者类的属性高级排序
- 数组里的值排序
- iOS给Model排序
- iOS-model(自定义)排序
- ios 后台返回json数据里还有数组集合时,model的配置,model套model
- iOS 根据数组里的model的属性进行筛选
- ios-高级排序
- IOS 数组的简单排序
- ios数组(NSArray)的排序
- 数组里的正数和负数排序
- 数组里的正数和负数排序
- iOS NSDictionary里的keysSortedByValueUsingSelector排序使用
- iOS根据model里面的时间字段进行排序
- 数组高级(排序和查找)
- iOS--字典数组排序(每个数组里面都是一个model)NSSortDescriptor
- php-fpm报502问题
- Buffer中的flip()
- Redis单核CPU占用过高
- SpringMVC 异常处理
- CMSIS-RTOS2 应用笔记 三 功能概述
- ios数组里的model排序(高级排序)
- 网络请求
- Python文件异常try/except/finally
- 【诗】诗词锦集(持续更新)
- CodeFroces 805B 3-palindrome
- jsp页面使用cookie记住密码 jquery
- 深入Collection集合的孙子接口HashSet
- [cv] linearity and convolution
- HDU