首页 > 代码库 > NSMutableDictionary 排序问题
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。