我有一个整数值的NSMutableDictionary
,我想获得一个按键排列,按各自的值升序排列。例如,用这本字典:获取按其各自的值排序的NSDictionary键
mutableDict = {
"A" = 2,
"B" = 4,
"C" = 3,
"D" = 1,
}
我想结束与阵列["D", "A", "C", "B"]
。当然,我的真实词典远远大于四项。
我有一个整数值的NSMutableDictionary
,我想获得一个按键排列,按各自的值升序排列。例如,用这本字典:获取按其各自的值排序的NSDictionary键
mutableDict = {
"A" = 2,
"B" = 4,
"C" = 3,
"D" = 1,
}
我想结束与阵列["D", "A", "C", "B"]
。当然,我的真实词典远远大于四项。
该NSDictionary
方法keysSortedByValueUsingComparator:
应该做的伎俩。
你只需要一个返回NSComparisonResult
的方法来比较对象的值。
你的字典是
NSMutableDictionary * myDict;
而且你的阵列是
NSArray *myArray;
myArray = [myDict keysSortedByValueUsingComparator: ^(id obj1, id obj2) {
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
只需使用NSNumber
对象而不是数字常量。
理查德的建议比我的更优雅,因为NSNumber已经有了一个合适的比较函数,但我的可能更一般。 – 2012-03-14 19:41:40
在大多数情况下,这个解决方案可以正常工作,但我的答案是支持响应'-compare:' – 2012-03-14 19:47:42
的其他类型的能力。这将返回密钥列表,是否可以直接获取字典,而不是有排序的键列表 – 2016-09-27 09:41:00
NSDictionary有这个叫做allKeys
整洁的方法。
如果你想要排序阵列,keysSortedByValueUsingComparator:
应该做的伎俩。
理查德的解决方案也适用,但使一些额外的电话,你不一定需要:
// Assuming myDictionary was previously populated with NSNumber values.
NSArray *orderedKeys = [myDictionary keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2){
return [obj1 compare:obj2];
}];
这里有一个解决方案:
NSDictionary *dictionary; // initialize dictionary
NSArray *sorted = [[dictionary allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [[dictionary objectForKey:obj1] compare:[dictionary objectForKey:obj2]];
}];
最简单的办法:
[dictionary keysSortedByValueUsingSelector:@selector(compare:)]
在这里我做了这样的事情:
NSMutableArray * weekDays = [[NSMutableArray alloc] initWithObjects:@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday", nil];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSMutableArray *dictArray = [[NSMutableArray alloc] init];
for(int i = 0; i < [weekDays count]; i++)
{
dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:i],@"WeekDay",[weekDays objectAtIndex:i],@"Name",nil];
[dictArray addObject:dict];
}
NSLog(@"Before Sorting : %@",dictArray);
@try
{
//for using NSSortDescriptor
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"WeekDay" ascending:YES];
NSArray *descriptor = @[sortDescriptor];
NSArray *sortedArray = [dictArray sortedArrayUsingDescriptors:descriptor];
NSLog(@"After Sorting : %@",sortedArray);
//for using predicate
//here i want to sort the value against weekday but only for WeekDay<=5
int count=5;
NSPredicate *Predicate = [NSPredicate predicateWithFormat:@"WeekDay <=%d",count];
NSArray *results = [dictArray filteredArrayUsingPredicate:Predicate];
NSLog(@"After Sorting using predicate : %@",results);
}
@catch (NSException *exception)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorting cant be done because of some error" message:[NSString stringWithFormat:@"%@",exception] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert setTag:500];
[alert show];
[alert release];
}
有几十个元素,我想要一个新的数组按键,按照包含整数的键的升序值排序 – Eric 2012-03-14 19:37:55
Josh,对于我所要做的,是的,他们都是整数。感谢大家的帮助! – Eric 2012-03-14 19:44:48