获取NSAroray的NSDictionary键,按Value然后键排序

时间:2022-11-14 13:42:37

Say I have a dictionary with keys (words) and values (scores) as follows:

假设我有一个包含键(单词)和值(分数)的字典,如下所示:

GOD    8
DONG   16
DOG    8
XI     21

I would like to create an NSArray of dictionary keys (words) that is sorted first by score then alphabetically. From the example above this would be:

我想创建一个字典键(单词)的NSArray,首先按分数排序,然后按字母顺序排序。从上面的例子可以看出:

XI
DONG
DOG
GOD

What's the best way to achieve this?

实现这一目标的最佳方法是什么?

3 个解决方案

#1


1  

I would use and NSArray of NSDictionaries and then implement it using NSSortDescriptors:

我会使用NSArray的NSArray然后使用NSSortDescriptors实现它:

NSSortDescriptor *sdScore = [NSSortDescriptor alloc] initWithKey:@"SCORE" ascending:NO];
NSSortDescriptor *sdName = [NSSortDescriptor alloc] initWithKey:@"NAME" ascending:YES];
NSArray *sortedArrayOfDic = [unsortedArrayOfDic sortedArrayUsingDescriptors:[NSArray arrayWithObjects: sdScore, sdName, nil]];

#2


0  

Carlos' answer is the correct one I'm just posting the full code I ended up with just in case anyone is interested:

卡洛斯的答案是正确的,我只是发布了我最终的完整代码以防万一有人感兴趣:

NSDictionary *dataSourceDict = [NSDictionary dictionaryWithObjectsAndKeys:
                      [NSNumber numberWithInt:8], @"GOD",
                      [NSNumber numberWithInt:16], @"DONG",
                      [NSNumber numberWithInt:8], @"DOG",
                      [NSNumber numberWithInt:21], @"XI", nil];

NSSortDescriptor *scoreSort = [NSSortDescriptor sortDescriptorWithKey:@"SCORE" ascending:NO];
NSSortDescriptor *wordSort = [NSSortDescriptor sortDescriptorWithKey:@"WORD" ascending:YES];
NSArray *sorts = [NSArray arrayWithObjects:scoreSort, wordSort, nil];


NSMutableArray *unsortedArrayOfDict = [NSMutableArray array];

for (NSString *word in dataSourceDict)
{
    NSString *score = [dataSourceDict objectForKey:word];
    [unsortedArrayOfDict addObject: [NSDictionary dictionaryWithObjectsAndKeys:word, @"WORD", score, @"SCORE",  nil]];
}
NSArray *sortedArrayOfDict = [unsortedArrayOfDict sortedArrayUsingDescriptors:sorts];

NSDictionary *sortedDict = [sortedArrayOfDict valueForKeyPath:@"WORD"];

NSLog(@"%@", sortedDict);

Related: NSDictionary split into two arrays (objects and keys) and then sorted both by the objects array (or a similar solution)

相关:NSDictionary分为两个数组(对象和键),然后由对象数组(或类似的解决方案)进行排序

#3


0  

I couldn't test this because i'm not on a Mac (sorry if I misspelled something), but:

我无法测试这个,因为我不在Mac上(抱歉,如果我拼错了一些东西),但是:

NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"GOD", @"WORD", [NSNumber numberWithInt:8], @"SCORE", nil];
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"DONG", @"WORD", [NSNumber numberWithInt:16], @"SCORE", nil];
NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"DOG", @"WORD", [NSNumber numberWithInt:8], @"SCORE", nil];
NSDictionary *dic4 = [NSDictionary dictionaryWithObjectsAndKeys:@"XI", @"WORD", [NSNumber numberWithInt:21], @"SCORE", nil];

NSSortDescriptor *scoreSort = [NSSortDescriptor sortDescriptorWithKey:@"SCORE" ascending:NO];
NSSortDescriptor *wordSort = [NSSortDescriptor sortDescriptorWithKey:@"WORD" ascending:YES];

NSArray *sortedArrayOfDic = [[NSArray arrayWithObjects:dic1, dic2, dic3, dic4, nil] sortedArrayUsingDescriptors:[NSArray arrayWithObjects:scoreSort, wordSort, nil]];

NSLog(@"%@", [sortedArrayOfDict valueForKeyPath:@"WORD"]);

This would do the same but a bit reduced and avoiding an iteration.

这会做同样的事情但有点减少并避免迭代。

#1


1  

I would use and NSArray of NSDictionaries and then implement it using NSSortDescriptors:

我会使用NSArray的NSArray然后使用NSSortDescriptors实现它:

NSSortDescriptor *sdScore = [NSSortDescriptor alloc] initWithKey:@"SCORE" ascending:NO];
NSSortDescriptor *sdName = [NSSortDescriptor alloc] initWithKey:@"NAME" ascending:YES];
NSArray *sortedArrayOfDic = [unsortedArrayOfDic sortedArrayUsingDescriptors:[NSArray arrayWithObjects: sdScore, sdName, nil]];

#2


0  

Carlos' answer is the correct one I'm just posting the full code I ended up with just in case anyone is interested:

卡洛斯的答案是正确的,我只是发布了我最终的完整代码以防万一有人感兴趣:

NSDictionary *dataSourceDict = [NSDictionary dictionaryWithObjectsAndKeys:
                      [NSNumber numberWithInt:8], @"GOD",
                      [NSNumber numberWithInt:16], @"DONG",
                      [NSNumber numberWithInt:8], @"DOG",
                      [NSNumber numberWithInt:21], @"XI", nil];

NSSortDescriptor *scoreSort = [NSSortDescriptor sortDescriptorWithKey:@"SCORE" ascending:NO];
NSSortDescriptor *wordSort = [NSSortDescriptor sortDescriptorWithKey:@"WORD" ascending:YES];
NSArray *sorts = [NSArray arrayWithObjects:scoreSort, wordSort, nil];


NSMutableArray *unsortedArrayOfDict = [NSMutableArray array];

for (NSString *word in dataSourceDict)
{
    NSString *score = [dataSourceDict objectForKey:word];
    [unsortedArrayOfDict addObject: [NSDictionary dictionaryWithObjectsAndKeys:word, @"WORD", score, @"SCORE",  nil]];
}
NSArray *sortedArrayOfDict = [unsortedArrayOfDict sortedArrayUsingDescriptors:sorts];

NSDictionary *sortedDict = [sortedArrayOfDict valueForKeyPath:@"WORD"];

NSLog(@"%@", sortedDict);

Related: NSDictionary split into two arrays (objects and keys) and then sorted both by the objects array (or a similar solution)

相关:NSDictionary分为两个数组(对象和键),然后由对象数组(或类似的解决方案)进行排序

#3


0  

I couldn't test this because i'm not on a Mac (sorry if I misspelled something), but:

我无法测试这个,因为我不在Mac上(抱歉,如果我拼错了一些东西),但是:

NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"GOD", @"WORD", [NSNumber numberWithInt:8], @"SCORE", nil];
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"DONG", @"WORD", [NSNumber numberWithInt:16], @"SCORE", nil];
NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"DOG", @"WORD", [NSNumber numberWithInt:8], @"SCORE", nil];
NSDictionary *dic4 = [NSDictionary dictionaryWithObjectsAndKeys:@"XI", @"WORD", [NSNumber numberWithInt:21], @"SCORE", nil];

NSSortDescriptor *scoreSort = [NSSortDescriptor sortDescriptorWithKey:@"SCORE" ascending:NO];
NSSortDescriptor *wordSort = [NSSortDescriptor sortDescriptorWithKey:@"WORD" ascending:YES];

NSArray *sortedArrayOfDic = [[NSArray arrayWithObjects:dic1, dic2, dic3, dic4, nil] sortedArrayUsingDescriptors:[NSArray arrayWithObjects:scoreSort, wordSort, nil]];

NSLog(@"%@", [sortedArrayOfDict valueForKeyPath:@"WORD"]);

This would do the same but a bit reduced and avoiding an iteration.

这会做同样的事情但有点减少并避免迭代。