0

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.

Alan Taylor
  • 493
  • 4
  • 16
  • Use actual numbers for your keys instead of strings and it will work just fine. – rmaddy Mar 20 '13 at 04:13
  • @maddy I realize that may make things easier for me, however there will be times when players will have a location that is off the game board and those locations ideally would not be represented as a number, so any way to do this with strings will prevent me from needing to make changes to that part of my program. – Alan Taylor Mar 20 '13 at 11:59

2 Answers2

1

Try this one:

NSArray *array = @[@"1",@"31",@"14",@"531",@"4",@"53",@"64",@"4",@"0"];

NSArray *sortedArray = [array sortedArrayUsingComparator:^(id str1, id str2) {
        return [((NSString *)str1) compare:((NSString *)str2) options:NSNumericSearch];
    }];
NSLog(@"%@",sortedArray);
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

Try This,

// 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]]]];
     }
NSArray *arrKeys = [[newDictionary allKeys];
NSArray *sortedArray = [arrKeys sortedArrayUsingComparator:^(id firstObject, id secondObject) {
    return [((NSString *)firstObject) compare:((NSString *)secondObject) options:NSNumericSearch];
}];
NSLog(@"%@",sortedArray);
Ravindhiran
  • 5,304
  • 9
  • 50
  • 82