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 排序问题的更多相关文章

  1. 编程之美—烙饼排序问题(JAVA)

    一.问题描述 星期五的晚上,一帮同事在希格玛大厦附近的"硬盘酒吧"多喝了几杯.程序员多喝了几杯之后谈什么呢?自然是算法问题.有个同事说:"我以前在餐      馆打工,顾 ...

  2. 字典NSDictionary以及NSMutableDictionary的用法总结

    做过Java语言 或者 C语言 开发的朋友应该很清楚 关键字map 吧,它可以将数据以键值对儿的形式储存起来,取值的时候通过KEY就可以直接拿到对应的值,非常方便.在Objective-C语言中 词典 ...

  3. iOS常用 --- NSDictionary 与 NSMutableDictionary

    一.NSDictionary 字典的两种创建方法 NSDictionary *dic1 =[[NSDictionary alloc]init]; 2 // 或: 3 NSDictionary *dic ...

  4. 关于SQL中的排序问题

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguratio ...

  5. 黑马程序员-NSDictionary和NSMutableDictionary

    NSDictionary和NSMutableDictionary:通过key和value进行对应,进行存储元素,能够方便提取所需的元素.key是不能够重复出现,但是value能够重复出现.NSDict ...

  6. distinct order by 排序问题

    使用类似“SELECT DISTINCT `col` FROM `tb_name` ORDER BY `time` DESC”这样的sql语句时,会遇到排序问题. 以上面的sql语句分析:order ...

  7. iOS阶段学习第15天笔记(NSDictionary与NSMutableDictionary 字典)

    iOS学习(OC语言)知识点整理 一.OC中的字典 1)字典:是一个容器对象,元素是以键-值对(key-value)形式存放的,key和value是任意类型的对象,key是唯一的,value可以重复 ...

  8. objective-c系列-NSDictionary&NSMutableDictionary

    ********************************************* NSDictionary ***************************************** ...

  9. OC第四节——NSDictionary和NSMutableDictionary

    NSDictionary    1.什么是字典        字典是也是一种集合结构,功能与我们现实中的字典工具一样    2.字典的元素是什么        任意类型的对象地址构成键值对    3. ...

随机推荐

  1. codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)

    题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...

  2. 单选框 复选框 隐藏之后,绑定的change事件在ie中失效的问题

    有时候需要对单选框和复选框进行美化,就需要在<input type="radio">和<input type="checkbox">元素 ...

  3. memset函数学习

    memset是计算机中C/C++语言函数.将s所指向的某一块内存中的后n个 字节的内容全部设置为ch指定的ASCII值, 第一个值为指定的内存地址,块的大小由第三个参数指定,这个函数通常为新申请的内存 ...

  4. Hackerearth: Mathison and the Pokémon fights

    Mathison and the Pokémon fights code 这是一道比较有意思,出的也非常好的题目. 给定$n$个平面上的点$(x_i, y_i)$,(允许离线地)维护$Q$个操作:1. ...

  5. 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时4

    课时4 数据驱动的图像分类:K最邻与线性分类器(上) 图像分类之前,我们需要将图片转换成一张巨大的数字表单,然后从所有种类中,给这个表单选定一个标签. 为什么分类问题是个困难的问题:图像分类难点是,当 ...

  6. Objective-C中的字符串格式化输出(转载)

    转自:http://www.cnblogs.com/jackbutler/archive/2012/04/05/2432828.html %@ 对象 %d, %i 整数 %u 无符整形 %f 浮点/双 ...

  7. 图像分类与KNN

    1 图像分类问题 1.1 什么是图像分类 所谓图像分类问题,就是已有固定的分类标签集合,然后对于输入的图像,从分类标签集合中找出一个分类标签,最后把分类标签分配给该输入图像.虽然看起来挺简单的,但这可 ...

  8. python property的2种使用方法

    一.property类 class Person(): def __init__(self, name): self.set_name(name) def get_name(self): return ...

  9. bzoj 4698: Sdoi2008 Sandy的卡片【SAM】

    差分之后用SAM求LCS,然后答案就是LCS+1 #include<iostream> #include<cstdio> #include<cstring> usi ...

  10. keepalived+nginx高可用实现

    1.keepalived介绍 keepalived最初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了实现高可用的VRRP功能.keepalived除了能 ...