NSDictionary分为两个数组(对象和键),然后根据对象数组(或类似的解决方案)对它们进行排序

时间:2022-02-27 12:17:51

I have an NSDictionary. It has keys and objects.

我有一个NSDictionary。它有键和对象。

For the purposes of simplicity the keys are Question numbers and the objects are calculated Answer scores.

为了简单起见,键是问号,对象是计算出的答案分数。

Now how I did it before was that I set the answer score as the keys and the question numbers as the objects. This way I could get an array of allKeys from the dictionary, sort it and then do something similar to:

我之前的做法是把答案分数设为键,把问题号设为对象。通过这种方式,我可以从字典中获取一个allkey数组,对它进行排序,然后做一些类似的事情:

for(NSString *string in tempArray){
  NSLog(@"%@",[dictionary objectForKey:string]);
}

The (stupid - on my part) problem that I have now encountered however is that (obviously... duuhhh) the keys need to unique, and therefore when the calculated answer scores are the same, only one answer gets output!

我现在遇到的(愚蠢的)问题是(显然……)关键字需要唯一,因此当计算的答案分数相同时,只有一个答案得到输出!

I need a solution to this. In PHP you can multisort arrays. I was wondering if there was some similar solution in objective-c or indeed if someone had a better answer?

我需要一个解决方法。在PHP中,您可以多排序数组。我想知道objective-c中是否有类似的解或者是否有人有更好的答案?

Any help here would be much appreciated.

如有任何帮助,我们将不胜感激。

Thank you.

谢谢你!

3 个解决方案

#1


2  

One solution is to store the answer scores using an array of dictionaries containing only two key-value pairs. One key is the question number (or however your questions are tagged, i.e. “Q1.1”), while the other key is the actual answer score. For example:

一种解决方案是使用只包含两个键值对的字典数组来存储答案分数。一个键是问题编号(或者你的问题被标记为“Q1.1”),另一个键是实际的答案分数。例如:

static NSString * const QuestionKey = @"questionNumber";
static NSString * const AnswerScoreKey = @"answerScore";

NSMutableArray *allAnswers = [NSMutableArray array];

for (int i = 0; i < 20; i++)
{
    // fill allAnswers array with random data
    NSDictionary *answer = [NSDictionary dictionaryWithObjectsForKeys:
        [NSString stringWithFormat:@"Q%d", i], QuestionKey,
        [NSNumber numberWithInt:rand()], AnswerScoreKey,
         nil];

    [allAnswers addObject:answer];
}

// sort the allAnswers array based on score, highest first
NSSortDescriptor *sortDesc = [NSSortDescriptor sortDescriptorWithKey:AnswerScoreKey ascending:NO];

[allAnswers sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];

for (NSDictionary *answer in allAnswers)
{
    NSLog(@"Question: %@, AnswerScore: %@", [answer objectForKey:QuestionKey], [answer objectForKey:AnswerScoreKey];
}

Disclaimer:

Untested and uncompiled code. Theory only.

未经检验的和没有编译的代码。理论。

#2


8  

Do you know about the allKeys, allValues and allKeysForObject method of the NSDictionary do you ?

你知道NSDictionary的allkey, allValues和allKeysForObject方法吗?

#3


0  

I am not sure what you are actually trying to achieve. Do you want to sort an array of dictionary objects based on the value of the dictionary? If that is what you want, you can define your own comparator function and can define any custom sorting behavior. Please check sortedArrayUsingSelector: method of NSArray.

我不确定你到底想要实现什么。您想要基于字典的值对字典对象数组进行排序吗?如果这是您想要的,您可以定义自己的comparator函数,并可以定义任何自定义排序行为。请检查sortedArrayUsingSelector: NSArray方法。

Edit : I am away from Mac currently but this previous question has a number of example code that can solve your problem. Rather than using object, you can use NSDictionary, though personally I would like to use another Question object instead of dictionary in this case. This question class will contain two value, id and score and then you need to sort the array of questions based on score just like the persons are sorted based on birthday.

编辑:我现在不使用Mac,但是前面的问题有一些示例代码可以解决您的问题。与使用object不同,您可以使用NSDictionary,不过我个人希望在本例中使用另一个问题对象而不是dictionary。这个问题类将包含两个值,id和score,然后你需要根据分数来排序问题的数组,就像根据生日来排序的人一样。

#1


2  

One solution is to store the answer scores using an array of dictionaries containing only two key-value pairs. One key is the question number (or however your questions are tagged, i.e. “Q1.1”), while the other key is the actual answer score. For example:

一种解决方案是使用只包含两个键值对的字典数组来存储答案分数。一个键是问题编号(或者你的问题被标记为“Q1.1”),另一个键是实际的答案分数。例如:

static NSString * const QuestionKey = @"questionNumber";
static NSString * const AnswerScoreKey = @"answerScore";

NSMutableArray *allAnswers = [NSMutableArray array];

for (int i = 0; i < 20; i++)
{
    // fill allAnswers array with random data
    NSDictionary *answer = [NSDictionary dictionaryWithObjectsForKeys:
        [NSString stringWithFormat:@"Q%d", i], QuestionKey,
        [NSNumber numberWithInt:rand()], AnswerScoreKey,
         nil];

    [allAnswers addObject:answer];
}

// sort the allAnswers array based on score, highest first
NSSortDescriptor *sortDesc = [NSSortDescriptor sortDescriptorWithKey:AnswerScoreKey ascending:NO];

[allAnswers sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];

for (NSDictionary *answer in allAnswers)
{
    NSLog(@"Question: %@, AnswerScore: %@", [answer objectForKey:QuestionKey], [answer objectForKey:AnswerScoreKey];
}

Disclaimer:

Untested and uncompiled code. Theory only.

未经检验的和没有编译的代码。理论。

#2


8  

Do you know about the allKeys, allValues and allKeysForObject method of the NSDictionary do you ?

你知道NSDictionary的allkey, allValues和allKeysForObject方法吗?

#3


0  

I am not sure what you are actually trying to achieve. Do you want to sort an array of dictionary objects based on the value of the dictionary? If that is what you want, you can define your own comparator function and can define any custom sorting behavior. Please check sortedArrayUsingSelector: method of NSArray.

我不确定你到底想要实现什么。您想要基于字典的值对字典对象数组进行排序吗?如果这是您想要的,您可以定义自己的comparator函数,并可以定义任何自定义排序行为。请检查sortedArrayUsingSelector: NSArray方法。

Edit : I am away from Mac currently but this previous question has a number of example code that can solve your problem. Rather than using object, you can use NSDictionary, though personally I would like to use another Question object instead of dictionary in this case. This question class will contain two value, id and score and then you need to sort the array of questions based on score just like the persons are sorted based on birthday.

编辑:我现在不使用Mac,但是前面的问题有一些示例代码可以解决您的问题。与使用object不同,您可以使用NSDictionary,不过我个人希望在本例中使用另一个问题对象而不是dictionary。这个问题类将包含两个值,id和score,然后你需要根据分数来排序问题的数组,就像根据生日来排序的人一样。