I am looking to make an array with items from a dictionary, and then order it in descending order so that the greatest value is at the top and the smallest is at the bottom. However, it seems to struggle when I have items that are more than one digit in length.
My code is as such:
// build a new dictionary to swap the values and keys around as my main dictionary stores these values in another way
NSMutableDictionary *newDictionary = [[NSMutableDictionary alloc] init];
for (int i = 1; i < (numberOfPlayers + 1); i++ ){
[newDictionary setValue:[NSString stringWithFormat:@"player%dSquareNumber", i] forKey:[NSString stringWithFormat:@"%@",[PlayerDictionary valueForKey:[NSString stringWithFormat:@"player%dSquareNumber", i]]]];
NSLog(@"value added to dictionary");
// my value should now look like "player1SquareNumber", and the key will be a number such as 8, 12, 32 etc
}
// build array to sort this new dictionary
NSArray *sortedKeys = [[newDictionary keysSortedByValueUsingSelector:@selector(compare:)] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
// make an array to sort based on this array
NSMutableArray *sortedValues = [NSMutableArray array];
for (NSString *key in sortedKeys){
[sortedValues addObject:[newDictionary objectForKey:key]];
}
NSLog(@"sortedValues = %@", sortedValues);
NSLog(@"sortedKeys = %@", sortedKeys);
My sorted keys should ideally be in numerical order, but what I am getting is an output like
10
11
18
7
8
For my sortedArrayUsingSelector:@selector()
I have tried a few different solutions such as compare:
caseInsensitiveCompare:
etc..
Any help here would be greatly appreciated!
EDIT+ I am aware that another question like this was asked. The solutions given are not designed for strings, and return the array in the ascending order instead of descending as a result. While I can work with this, I was hoping to learn here how I could work with strings and still get the array in the order that I was hoping for.