IT门户网,专注电脑知识与技术及手机硬件信息服务网站!

当前位置:IT门户网 > 系统教程 >

iOS 字典凭据key的排序

时间:2021-09-14    来源:IT门户网    人气:

-(NSString*)stringWithDict:(NSDictionary*)dict{ NSArray*keys = [dict allKeys]; NSArray*sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2) { return[obj1 compare:obj2 options:NSNumericSearch];//正序 }]; NSString*str =@""; for(NSString*categoryId in sortedArray) { id value = [dict objectForKey:categoryId]; if([value isKindOfClass:[NSDictionary class]]) { value = [self stringWithDict:value]; } if([str length] !=0) { str = [str stringByAppendingString:@","]; } str = [str stringByAppendingFormat:@"%@:%@",categoryId,value]; } NSLog(@"str:%@",str); return str;}要领二

-(NSString *)getNeedSignStrFrom:(id)obj{ NSDictionary *dict = obj; NSArray *arrPRimary = dict.allKeys; NSArray *arrKey = [arrPrimary sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){ NSComparisonResult result = [obj1 compare:obj2]; return result==NSOrderedDescending;//NSOrderedAscending 倒序 }]; NSString*str =@""; for (NSString *s in arrKey) { id value = dict[s]; if([value isKindOfClass:[NSDictionary class]]) { value = [self getNeedSignStrFrom:value]; } if([str length] !=0) { str = [str stringByAppendingString:@","]; } str = [str stringByAppendingFormat:@"%@:%@",s,value]; } NSLog(@"str:%@",str); return str;}

要领一是网上的

主要是排序的方法略有差异 运用递归的要领感受挺好的

相关文章

系统教程排行榜

更多>>

网络知识排行榜

更多>>

系统教程排行榜

更多>>

服务号