NSMutableDictionary 排序问题
NSMutableDictionary 默认情况下是按字母的顺序进行排序的 (a-z)的默认排序
如何自定义排序呢?
第一种,利用数组的sortedArrayUsingComparator调用 NSComparator ,obj1和obj2指的数组中的对象
示例:
//声明一个数组
NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];
//排序前输出
NSMutableString *outputBefore = [[NSMutableString alloc] init];
for(NSString *str in sortArray){
[outputBefore appendFormat:@"];
}
NSLog(@"排序前:%@",outputBefore); //调用sortedArrayUsingComparator排序后
NSArray *array = [sortArray sortedArrayUsingComparator:cmptr];
NSMutableString *outputAfter = [[NSMutableString alloc] init];
for(NSString *str in array){
[outputAfter appendFormat:@"%@",str];
}
NSLog(@"排序后:%@",outputAfter); //调用的排序的方法
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;
};
第二种 排序方法 利用sortedArrayUsingFunction 调用 对应方法customSort,这个方法中的obj1和obj2分别是指数组中的对象。
NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];
//排序前
NSMutableString *outputBefore = [[NSMutableString alloc] init];
for(NSString *str in sortArray){
[outputBefore appendFormat:@"];
}
NSLog(@"排序前:%@",outputBefore); NSArray *array = [sortArray sortedArrayUsingFunction:customSort context:nil]; NSMutableString *outputAfter = [[NSMutableString alloc] init];
for(NSString *str in array){
[outputAfter appendFormat:@"];
}
NSLog(@"排序后:%@",outputAfter); NSInteger customSort(id obj1, id obj2,void* context){
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
} if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}
第三种 利用sortUsingDescriptors调用NSSortDescriptor
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"price"
ascending:NO];//其中,price为数组中的对象的属性,这个针对数组中存放对象比较更简洁方便
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
[_totalInfoArray sortUsingDescriptors:sortDescriptors];
[_airListView refreshTable:_totalInfoArray];
字符串的比较模式:
NSComparator cmptr = ^(id obj1, id obj2){
if([[NSString stringWithFormat:@"%@",obj1] compare:[NSString stringWithFormat:@"%@",obj2] options:NSNumericSearch] > 0)
{
return (NSComparisonResult)NSOrderedDescending;
}
if([[NSString stringWithFormat:@"%@",obj1] compare:[NSString stringWithFormat:@"%@",obj2] options:NSNumericSearch] < 0)
{
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
};
数字比较模式:
NSInteger customSort(id obj1, id obj2,void* context){
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}
本文来自:http://www.gowhich.com/blog/177
NSMutableDictionary 排序问题的更多相关文章
- 编程之美—烙饼排序问题(JAVA)
一.问题描述 星期五的晚上,一帮同事在希格玛大厦附近的"硬盘酒吧"多喝了几杯.程序员多喝了几杯之后谈什么呢?自然是算法问题.有个同事说:"我以前在餐 馆打工,顾 ...
- 字典NSDictionary以及NSMutableDictionary的用法总结
做过Java语言 或者 C语言 开发的朋友应该很清楚 关键字map 吧,它可以将数据以键值对儿的形式储存起来,取值的时候通过KEY就可以直接拿到对应的值,非常方便.在Objective-C语言中 词典 ...
- iOS常用 --- NSDictionary 与 NSMutableDictionary
一.NSDictionary 字典的两种创建方法 NSDictionary *dic1 =[[NSDictionary alloc]init]; 2 // 或: 3 NSDictionary *dic ...
- 关于SQL中的排序问题
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguratio ...
- 黑马程序员-NSDictionary和NSMutableDictionary
NSDictionary和NSMutableDictionary:通过key和value进行对应,进行存储元素,能够方便提取所需的元素.key是不能够重复出现,但是value能够重复出现.NSDict ...
- distinct order by 排序问题
使用类似“SELECT DISTINCT `col` FROM `tb_name` ORDER BY `time` DESC”这样的sql语句时,会遇到排序问题. 以上面的sql语句分析:order ...
- iOS阶段学习第15天笔记(NSDictionary与NSMutableDictionary 字典)
iOS学习(OC语言)知识点整理 一.OC中的字典 1)字典:是一个容器对象,元素是以键-值对(key-value)形式存放的,key和value是任意类型的对象,key是唯一的,value可以重复 ...
- objective-c系列-NSDictionary&NSMutableDictionary
********************************************* NSDictionary ***************************************** ...
- OC第四节——NSDictionary和NSMutableDictionary
NSDictionary 1.什么是字典 字典是也是一种集合结构,功能与我们现实中的字典工具一样 2.字典的元素是什么 任意类型的对象地址构成键值对 3. ...
随机推荐
- 【CAIOJ 1178】 最长共同前缀长度
[题目链接] 点击打开链接 [算法] EXKMP [代码] #include<bits/stdc++.h> using namespace std; #define MAXL 100001 ...
- python-----实现print不换行
python中print输出是默认换行的,那如何我们不想换行,且需要用多个print函数输出时,就需要改变print默认换行的属性: 例: print('Hello') print('!') 输出结果 ...
- python+selenium高亮显示正在操作的页面元素
原文地址:https://blog.csdn.net/wxstar8/article/details/80801405 from selenium import webdriver import un ...
- 【转】[钉钉通知系列]Jenkins发布后自动通知
转载请注明出处:https://www.cnblogs.com/jianxuanbing/p/7211006.html 阅读目录 一.前言 二.使用钉钉推送的优势 三.配置 一.前言 最近使用Jenk ...
- 移动web开发-------meta
<meta content=”width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0″ name=”v ...
- Logback中%X的使用
1. 参考资料 https://gist.github.com/logogin/ff44c254f655340b653c http://www.cnblogs.com/zhudongchang/p/6 ...
- 与adb相关的问题,比如掉线问题、Android Studio 提示Session 'app':Error Installing APK、找不到设备
这一篇帖子 会写的比较简单 不过相信大家也可能遇到这些问题 为了怕自己忘记 记录下来 顺便也和大家一起分享 描述:在一些机型上安装软件 提示卸载原先的软件 但是又安装不上新软件 DELETE ...
- 跟我一起玩Win32开发(5):具有单选标记的菜单
帅哥们,美女们,下午好,我又来误人子弟,请做好准备. 今天,我们的目的是,想要实现下图中的这种菜单效果. 就是一种类似单选按钮的菜单,多个菜单项中,同时只有一个会被选中. 首先,我们在资源编辑器中,设 ...
- python实现基数排序
# 基数排序有着局限性,只能是整数,# 排序的时候要先排后面一个条件的(多条件排序)#如本例中,先从个位开始排起# 多关键字排序# 从低关键字开始排序 # @File: radix_sort #### ...
- 接口测试_RESTClient基本使用
火狐浏览器插件RESTClient基本使用. 消息头: Content-Type : application/x-www-form-urlencoded