
So first the entries into the NSDictionary... (this is tested and working as expected)
case TurnOrderBidding: [self.match.state.turnBids setObject:[NSNumber numberWithInt:self.match.state.numberFieldInt] forKey:[HelperFunctions formatTypeToString:self.match.state.currentPlayer.playerID]]; NSLog(@"Bid added from %@: %d", [HelperFunctions formatTypeToString:self.match.state.currentPlayer.playerID], [(NSNumber *)[self.match.state.turnBids objectForKey:[HelperFunctions formatTypeToString:self.match.state.currentPlayer.playerID]] intValue]); if(self.match.state.turnBids.count == self.match.state.players.count) { [self.match setTurnOrder]; NSLog(@"Keys in order:%@", self.match.state.turnBids); } else { [self.match.state setNumberFieldInt:0]; [self.numberFieldLabel.label setString:[NSString stringWithFormat:@"%d",self.match.state.numberFieldInt]]; [self.match nextPlayer]; } break;
And here is the setTurnOrder method:
-(void)setTurnOrder { self.state.turnOrder = [self.state.turnBids keysSortedByValueUsingComparator: ^(id obj1, id obj2) { NSLog(@"Comparing: %d and %d",[obj1 integerValue] , [obj2 integerValue]); if ([obj1 integerValue] > [obj2 integerValue]) { NSLog(@"Returning: %d", (NSComparisonResult)NSOrderedDescending); return (NSComparisonResult)NSOrderedDescending; } if ([obj1 integerValue] < [obj2 integerValue]) { NSLog(@"Returning: %d", (NSComparisonResult)NSOrderedAscending); return (NSComparisonResult)NSOrderedAscending; } NSLog(@"Returning: %d", (NSComparisonResult)NSOrderedAscending); return (NSComparisonResult)NSOrderedSame; }]; }
Here is the output for the above...
2013-04-25 04:36:01.296 2210[4984:c07] Bid added from Yellow: 2
2013-04-25 04:36:06.346 2210[4984:c07] Bid added from Red: 1
2013-04-25 04:36:10.897 2210[4984:c07] Bid added from Black: 4
2013-04-25 04:36:16.697 2210[4984:c07] Bid added from Green: 3
2013-04-25 04:36:16.698 2210[4984:c07] Comparing: 4 and 2
2013-04-25 04:36:16.698 2210[4984:c07] Returning: 1
2013-04-25 04:36:16.699 2210[4984:c07] Comparing: 3 and 1
2013-04-25 04:36:16.699 2210[4984:c07] Returning: 1
2013-04-25 04:36:16.700 2210[4984:c07] Comparing: 2 and 1
2013-04-25 04:36:16.700 2210[4984:c07] Returning: 1
2013-04-25 04:36:16.701 2210[4984:c07] Comparing: 2 and 3
2013-04-25 04:36:16.702 2210[4984:c07] Returning: -1
2013-04-25 04:36:16.702 2210[4984:c07] Comparing: 4 and 3
2013-04-25 04:36:16.702 2210[4984:c07] Returning: 1
2013-04-25 04:36:16.703 2210[4984:c07] Keys in order:{
Black = 4;
Green = 3;
Red = 1;
Yellow = 2;
}
As you can see it seems like it's sorting the values (the comparing/returning output) but it just ends up sorting the keys by alphabetical order instead of sorting them by the value (bid) associated with the key. What am I missing here?
Sam
OMG! I was printing out the wrong array for the results! It's been working this whole time! Time for bed
