开发者

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

开发者 https://www.devze.com 2023-01-17 05:08 出处:网络
I have an NSDictionary. It has keys and objects. For the purposes of simplicity the keys are Question numbers and the objects are calculated Answer scores.

I have an NSDictionary. It has keys and objects.

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:

for(NSString *string in tempArray){
  NSLog(@"开发者_Python百科%@",[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?

Any help here would be much appreciated.

Thank you.


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


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:

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.


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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消