2012-03-14 65 views
39

我有一个整数值的NSMutableDictionary,我想获得一个按键排列,按各自的值升序排列。例如,用这本字典:获取按其各自的值排序的NSDictionary键

mutableDict = { 
    "A" = 2, 
    "B" = 4, 
    "C" = 3, 
    "D" = 1, 
} 

我想结束与阵列["D", "A", "C", "B"]。当然,我的真实词典远远大于四项。

+0

有几十个元素,我想要一个新的数组按键,按照包含整数的键的升序值排序 – Eric 2012-03-14 19:37:55

+0

Josh,对于我所要做的,是的,他们都是整数。感谢大家的帮助! – Eric 2012-03-14 19:44:48

回答

62

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对象而不是数字常量。

顺便说一句,这是取自: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Collections/Articles/Dictionaries.html

+4

理查德的建议比我的更优雅,因为NSNumber已经有了一个合适的比较函数,但我的可能更一般。 – 2012-03-14 19:41:40

+1

在大多数情况下,这个解决方案可以正常工作,但我的答案是支持响应'-compare:' – 2012-03-14 19:47:42

+0

的其他类型的能力。这将返回密钥列表,是否可以直接获取字典,而不是有排序的键列表 – 2016-09-27 09:41:00

26

NSDictionary有这个叫做allKeys整洁的方法。

如果你想要排序阵列,keysSortedByValueUsingComparator:应该做的伎俩。

理查德的解决方案也适用,但使一些额外的电话,你不一定需要:

// Assuming myDictionary was previously populated with NSNumber values. 
NSArray *orderedKeys = [myDictionary keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2){ 
    return [obj1 compare:obj2]; 
}]; 
+0

我编辑了我的问题,我认为这是暗示钥匙必须按顺序键值 – Eric 2012-03-14 19:30:11

+0

我在寻求排序帮助。 – Eric 2012-03-14 19:32:31

+0

'keysSortedByValueUsingComparator:'会做这种排序。 – 2012-03-14 19:34:30

14

这里有一个解决方案:

NSDictionary *dictionary; // initialize dictionary 
NSArray *sorted = [[dictionary allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { 
    return [[dictionary objectForKey:obj1] compare:[dictionary objectForKey:obj2]]; 
}]; 
+0

只适用于alphabates开头的字符串,但是当有一个以数字10开头的字符串时,第一个字符串以“A”开头,第三个字符串以“A”开头之间的联系 – ViruMax 2014-03-10 06:57:34

+0

检查上面的答案,它更直观和简洁,尽管这是做同样的事情。 – nemesis 2014-09-01 16:25:24

13

最简单的办法:

[dictionary keysSortedByValueUsingSelector:@selector(compare:)]

+1

如何编写比较函数 – 2015-06-10 14:20:49

+0

请说明比较方法。 – Tuhin 2015-10-05 05:43:26

+0

对于现有的类型(例如NSString),该方法已经存在。对于您自己创建的任何内容,该方法应根据顺序返回NSOrderedAscending,NSOrderedSame或NSOrderedDescending。有关详细信息,请参阅“比较:选项:范围:”的文档。 – dgatwood 2016-10-07 22:12:39

2

在这里我做了这样的事情:

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]; 
}