3

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(@"%@",[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.

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
  • Hi again Thomas ! WHat exactly do you need as a result of this treatment ? Please give more details on the context. – VdesmedT Sep 24 '10 at 09:37

3 Answers3

8

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

VdesmedT
  • 9,037
  • 3
  • 34
  • 50
  • so I could get "allValues" into an array, then sort that then use "allKeysForObject" to get an array of question numbers that have that particular answer? Then my only problem is duplicate answers. Because if I always use allKeysForObject then I everytime I encounter a duplicate answer it will output all of the questions. – Thomas Clayson Sep 24 '10 at 09:54
  • @Thomas Clayson: Isn't that what you want though? If questions 5.15 and 6.3 both had answer scores of 21, you can't differentiate them by sorting on answer score. Nor do you need to be able to. – JeremyP Sep 24 '10 at 11:11
  • yeah i know, but every time I get to 21 it will give me all of the questions - I guess I could just make sure 21 dosen't come up more than once. :p – Thomas Clayson Sep 24 '10 at 11:21
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:

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.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • thats very complicated haha :p I've still yet to get my head round it. :p Will have a look and see if I can use it. Thank you. :) – Thomas Clayson Sep 24 '10 at 09:59
  • @Thomas: It appears complicated at first, but the first half of the code is simply creating dummy values to sort. The latter half is the more important part as it shows how to sort data using sort descriptors and key paths. Structuring your data properly will reduce inefficiencies in performance and increase maintainability. – dreamlax Sep 24 '10 at 10:01
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.

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.

Community
  • 1
  • 1
taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • i have questions from 1 - 10 lets say. And I have scores for each question. For instance 0.122452, 0.148838, 0.2199938, 1.234884 etc etc etc. I want to RANK the questions by score. For instance if question 1 = 1.4 and question 2 = 1.5 then I want to (somehow) know that question 2 is first and question 1 is second. – Thomas Clayson Sep 24 '10 at 09:52