Hi all,
I am stuck on something which ought to be really simple. ( I think!!!)
I have an NSDictionary which stores the distribution of characters in a string.
In order to sort the dictionary so that it can be displayed in a tableView, I have used the method:
where "characterList" is the dictionary. This sorts the array by value, ( where value is an NSNumber, which is what I want, but it is being sorted in the inverse order to the way I want it displayed. This has seemed to confuse quite a few as many questions have been asked to which all the answers have been: "Read the documentation". Well, the docs are quite clear as to what is happening,
but, as one expects, the compiler rejects this as key is undeclared.
Anyway, some insight would be greatly appreciated. Thanks as always in advance.
I am stuck on something which ought to be really simple. ( I think!!!)
I have an NSDictionary which stores the distribution of characters in a string.
In order to sort the dictionary so that it can be displayed in a tableView, I have used the method:
Code:
return [[ self characterList] keysSortedByValueUsingSelector:@selector(compare:)];
where "characterList" is the dictionary. This sorts the array by value, ( where value is an NSNumber, which is what I want, but it is being sorted in the inverse order to the way I want it displayed. This has seemed to confuse quite a few as many questions have been asked to which all the answers have been: "Read the documentation". Well, the docs are quite clear as to what is happening,
So, conceptually, I get it ...I think. I have tried to write a custom method "inverseCompare" but get stuck as to understanding exactly how cocoa wishes the comparison presented. My idea would be simply to reverse the comparison, so something like this.Discussion
Pairs of dictionary values are compared using the comparison method specified by comparator; the comparator message is sent to one of the values and has as its single argument the other value from the dictionary.
Code:
-(NSComparisonResult)inverseCompare: (NSNumber *) n
{
return [n compare:[[self characterList]objectForKey:key]];
}
Anyway, some insight would be greatly appreciated. Thanks as always in advance.