iphone开发数组排序(数组中包括字典)
2016-05-12 11:12
537 查看
原文地址
http://blog.csdn.net/bihailantian1988/article/details/8433812iphone开发数组排序(数组中包括字典)
1。普通数组排序:NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"0",@"8",@"6",@"1",nil];
NSArray *sortedArray = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
if ([obj1 intValue] > [obj2 intValue]){
return NSOrderedDescending;
}
if ([obj1 intValue] < [obj2 intValue]){
return NSOrderedAscending;
}
return NSOrderedSame;
}];
NSLog(@"排序后的数组:%@",sortedArray);
2.数组中有字典的排序:
NSMutableArray *myMutableArr = [[[NSMutableArray alloc] init] autorelease];
NSDictionary *dicOne = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"price",@"2",@"number", nil];
NSDictionary *dicTWo = [NSDictionary dictionaryWithObjectsAndKeys:@"6",@"price",@"5",@"number", nil];
NSDictionary *dicThree = [NSDictionary dictionaryWithObjectsAndKeys:@"3",@"price",@"1",@"number", nil];
NSDictionary *dicFour = [NSDictionary dictionaryWithObjectsAndKeys:@"4",@"price",@"3",@"number", nil];
[myMutableArr addObject:dicOne];
[myMutableArr addObject:dicTWo];
[myMutableArr addObject:dicThree];
[myMutableArr addObject:dicFour];
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES]];
[myMutableArr sortUsingDescriptors:sortDescriptors];
NSLog(@"排序后的数组%@",myMutableArr);
相关文章推荐
- 绝大部分 Android 手电筒应用需要大量权限
- 这些看似合法的 iPhone Lightning 数据线将劫持您的电脑
- 如何成为一名专家级的开发人员
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- gitinspector+jenkins 开发代码统计CI
- 我的iPhone桌面
- .NET微信公众号开发之公众号消息处理
- Node.js开发者必须了解的4个JS要点
- 使用nodejs开发cli项目实例
- 谷歌Chrome浏览器扩展程序开发小记
- 三种检测iPhone/iPad设备方向的方法
- js实现iPhone界面风格的单选框和复选框按钮实例
- nodejs开发微博实例
- Egret引擎开发指南之创建项目
- Android开发自学笔记(二):工程文件剖析
- Java开发人员需知的十大戒律
- Web 开发常用工具 大家自己查找下载
- 三种Web开发主流技术的性价评价