在日常开发中,经常会遇到根据字典的Key对Value进行排序的问题,所以对于此类问题抽出一个通用方法,废话不多说,上代码.
/**
对字典(Key-Value)排序 区分大小写
@param dict 要排序的字典
*/
- (void)sortedDictionary:(NSDictionary *)dict{
//将所有的key放进数组
NSArray *allKeyArray = [dict allKeys];
//序列化器对数组进行排序的block 返回值为排序后的数组
NSArray *afterSortKeyArray = [allKeyArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id
Nonnull obj2) {
/**
In the compare: methods, the range argument specifies the
subrange, rather than the whole, of the receiver to use in the
comparison. The range is not applied to the search string. For
example, [@"AB" compare:@"ABC" options:0 range:NSMakeRange(0,1)]
compares "A" to "ABC", not "A" to "A", and will return
NSOrderedAscending. It is an error to specify a range that is
outside of the receiver's bounds, and an exception may be raised.
- (NSComparisonResult)compare:(NSString *)string;
compare方法的比较原理为,依次比较当前字符串的第一个字母:
如果不同,按照输出排序结果
如果相同,依次比较当前字符串的下一个字母(这里是第二个)
以此类推
排序结果
NSComparisonResult resuest = [obj1 compare:obj2];为从小到大,即升序;
NSComparisonResult resuest = [obj2 compare:obj1];为从大到小,即降序;
注意:compare方法是区分大小写的,即按照ASCII排序
*/
//排序操作
NSComparisonResult resuest = [obj1 compare:obj2];
return resuest;
}];
NSLog(@"afterSortKeyArray:%@",afterSortKeyArray);
//通过排列的key值获取value
NSMutableArray *valueArray = [NSMutableArray array];
for (NSString *sortsing in afterSortKeyArray) {
NSString *valueString = [dict objectForKey:sortsing];
[valueArray addObject:valueString];
}
NSLog(@"valueArray:%@",valueArray);
}
代码中提到了排序是按照ASCII码排序,因为ASCII码表示区分大小写的,所以我们的排序结果也区分大小写.
ASCII表.jpg如果不想区分大小写,可以在比较前,可以用lowercaseString方法将字符串转换为小写.代码如下
/**
对字典(Key-Value)排序 不区分大小写
@param dict 要排序的字典
*/
- (void)sortedDictionarybyLowercaseString:(NSDictionary *)dict{
//将所有的key放进数组
NSArray *allKeyArray = [dict allKeys];
//序列化器对数组进行排序的block 返回值为排序后的数组
NSArray *afterSortKeyArray = [allKeyArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id
Nonnull obj2) {
/**
In the compare: methods, the range argument specifies the
subrange, rather than the whole, of the receiver to use in the
comparison. The range is not applied to the search string. For
example, [@"AB" compare:@"ABC" options:0 range:NSMakeRange(0,1)]
compares "A" to "ABC", not "A" to "A", and will return
NSOrderedAscending. It is an error to specify a range that is
outside of the receiver's bounds, and an exception may be raised.
- (NSComparisonResult)compare:(NSString *)string;
compare方法的比较原理为,依次比较当前字符串的第一个字母:
如果不同,按照输出排序结果
如果相同,依次比较当前字符串的下一个字母(这里是第二个)
以此类推
排序结果
NSComparisonResult resuest = [obj1 compare:obj2];为从小到大,即升序;
NSComparisonResult resuest = [obj2 compare:obj1];为从大到小,即降序;
注意:compare方法是区分大小写的,即按照ASCII排序
*/
//小写转化
obj1 = [obj1 lowercaseString];
obj2 = [obj2 lowercaseString];
//排序操作
NSComparisonResult resuest = [obj1 compare:obj2];
return resuest;
}];
NSLog(@"afterSortKeyArray:%@",afterSortKeyArray);
//通过排列的key值获取value
NSMutableArray *valueArray = [NSMutableArray array];
for (NSString *sortsing in afterSortKeyArray) {
NSString *valueString = [dict objectForKey:sortsing];
[valueArray addObject:valueString];
}
NSLog(@"valueArray:%@",valueArray);
}
简单测试代码
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Danny",@"Basd",@"12",@"Cerff",@"man",@"ABCDEFG",@"Sean",@"Cerf",nil];
NSLog(@"dictionary:%@",dict);
[self sortedDictionary:dict];
输出为:
2017-02-12 11:23:03.484 根据key对dictionary排序[6122:164377] dictionary:{
ABCDEFG = 12345;
Basd = Rddd;
Cerf = Sean;
Cerff = iiiii;
}
2017-02-12 11:23:03.484 根据key对dictionary排序[6122:164377] afterSortKeyArray:(
ABCDEFG,
Basd,
Cerf,
Cerff
)
2017-02-12 11:23:03.485 根据key对dictionary排序[6122:164377] valueArray:(
12345,
Rddd,
Sean,
iiiii
)
网友评论