views:

105

answers:

3

Hi there

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.

+1  A: 

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

VdesmedT
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
dunno why someone downvoted you +1 for trying to help :)
Thomas Clayson
@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
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
A: 

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.

taskinoor
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
please check the edit
taskinoor
+2  A: 
dreamlax
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
@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