利用KVC的 valueForKeyPath
API、谓词的NSPredicate、数组的sortedArrayUsingComparator:
API进行实现。
/**
递归
- 处理key对应的Value是字典的情况
request body参数名ASCII码从小到大排序(字典序),
使用URL键值对的格式拼接成字符串 (key1=value1&key2=value2…)
*/
+ (NSString *)sortedDictionary:(NSMutableDictionary *)originParam {
NSDictionary *dict = originParam;
NSMutableString *contentString =[NSMutableString string];
NSArray *keys = [dict allKeys];
//按字母顺序排序
NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2 options:NSNumericSearch];///*options:NSNumericSearch Added in 10.2; Numbers within strings are compared using numeric value, that is, Foo2.txt < Foo7.txt < Foo25.txt; only applies to compare methods, not find */
}];
//拼接字符串
for (NSString *categoryId in sortedArray) {
#pragma mark - ******** todo value区分数组[,]、字符串、字段对象{}
id categoryIdV = [dict objectForKey:categoryId];
//
if( [categoryIdV isKindOfClass:NSString.class]){
}
else if( [categoryIdV isKindOfClass:NSDictionary.class]){
categoryIdV = [self sortedDictionary:categoryIdV];
categoryIdV = [NSString stringWithFormat:@"%@%@%@",@"{",categoryIdV,@"}"];
}
// 数组就[]
else if( [categoryIdV isKindOfClass:NSArray.class]){
categoryIdV = [self sortedDictionaryArr:categoryIdV];
}
if ([contentString length] <= 0) {
//第一个参数
}else{
[contentString appendString:@"&"];
}
[contentString appendFormat:@"%@=%@", categoryId,categoryIdV ];
}
NSLog(@"%@",contentString);
return contentString;
}
使用谓词进行数据分组 (数组元素为 自定义类型)
iOS NSPredicate的应用指南之【从数组搜索特定条件的元素】(从数组中筛选type=8的电子签名数据,避免遍历数组 certificateInfoList)
//2.按照日期进行分组展示
// 重新按照日期进行分组组织数据
// 找出不重复的日期
/**
对象操作:
针对数组的情况
@distinctUnionOfObjects:返回指定属性去重后的值的数组
@unionOfObjects:返回指定属性的值的数组,不去重
属性的值不能为空,否则产生异常。
*/
//1)取出日期分组
NSArray *arDistinct = [maTemp valueForKeyPath:@"@distinctUnionOfObjects.strDateCreated"];//maTemp是一些含有日期属性的对象集合
//2)构建排序规则NSComparator
NSComparator cmptr = ^(id obj1, id obj2){
NSString *strData1 = obj1;
NSString *strData2 = obj2;
NSComparisonResult ret = [strData1 compare:strData2];
return ret;
};
// 3)按数字从小到大进行排序(将最新的数据显示在前面)
NSArray *arSorted = [arDistinct sortedArrayUsingComparator:cmptr];
arSorted = arSorted.reverseObjectEnumerator.allObjects;//顺序取反
NSPredicate *predicate = nil;
// 4)按照日期进行分组
for (NSString *strDateCreated in arSorted)
{
predicate = [NSPredicate predicateWithFormat:@"strDateCreated == %@", strDateCreated];
NSArray *arFiltered = [maTemp filteredArrayUsingPredicate:predicate];//以一定的条件(特定日期)过滤maTemp数组,即进行大数据搜索。
OrderQueryGroupData *data = [OrderQueryGroupData alloc];
data.strDateCreated = strDateCreated;
data.maOrderData = [NSMutableArray arrayWithArray:arFiltered];
data.maOrderDataF = [IPOrderDataFrame frameListWith:data.maOrderData];
[self.maOrderQuery addObject:data];
}
需求:对高德SDK返回的当前位置POI数组按照距离排序
+(NSString*)POInamebyArr:(NSArray*)arr{
// 返回距离目标地址最近的POI:对元素根据distance属性进行排序,返回第一个(distance当前POI与当前位置的距离)
NSArray *sortedArray = [arr sortedArrayUsingComparator:^NSComparisonResult(TencentLBSPoi* obj1, TencentLBSPoi* obj2) {
return [[NSNumber numberWithDouble:obj1.distance].description compare:[NSNumber numberWithDouble: obj2.distance].description options:NSNumericSearch];
///
///
}];
//————————————————
//版权声明:本文为CSDN博主「iOS逆向」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
//原文链接:https://blog.csdn.net/z929118967/article/details/115242819
TencentLBSPoi *firstObject = sortedArray.firstObject;
return firstObject.name;
}
/**
IOS中将对象数组按照时间戳排序
weakSelf.viewModel.listModels= [QCT_Common sortedArrayUsingObjectKey:@"createTime" arr:weakSelf.viewModel.listModels];
*/
- (NSMutableArray*)sortedArrayUsingObjectKey:(NSString*)key arr:(NSArray*)listModels{
//1)取出日期分组
NSString* valueForKeyPath = FMSTR(@"@distinctUnionOfObjects.%@",key);
NSArray *arDistinct = [listModels valueForKeyPath:valueForKeyPath];
//listModels是一些含有日期属性的对象集合
//2)构建排序规则NSComparator
NSComparator cmptr = ^(id obj1, id obj2){
NSString *strData1 = obj1;
NSString *strData2 = obj2;
NSComparisonResult ret = [strData1 compare:strData2];
return ret;
};
// 3)按数字从小到大进行排序(将最新的数据显示在前面)
NSArray *arSorted = [arDistinct sortedArrayUsingComparator:cmptr];
arSorted = arSorted.reverseObjectEnumerator.allObjects;//顺序取反
NSMutableArray *newSorted_arr = [NSMutableArray array];
// 4)按照日期进行分组
for (NSString *strDateCreated in arSorted)
{
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"createTime == %@", strDateCreated];
NSArray *arFiltered = [listModels filteredArrayUsingPredicate:predicate];//以一定的条件(特定日期)过滤maTemp数组,即进行大数据搜索。
[newSorted_arr addObjectsFromArray:arFiltered];
}
return newSorted_arr;
// ————————————————
// 版权声明:本文为CSDN博主「#公众号:iOS逆向」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
// 原文链接:https://blog.csdn.net/z929118967/article/details/115242819