Dale Townsend
Dale Townsend

Reputation: 691

Determine minimum value in array and find corresponding string in another array

I have a 'winners screen' in a mini-golf scorecard app I'm making. I have two arrays that store 1). The player's names and 2). Their scores.

playerNameArray = [[NSMutableArray alloc]initWithObjects:playerOneName.text, playerTwoName.text, playerThreeName.text, playerFourName.text, playerFiveName.text, playerSixName.text, nil];
scoreArray = [[NSMutableArray alloc]initWithObjects:playerOneStat.text, playerTwoStat.text, playerThreeStat.text, playerFourStat.text, playerFiveStat.text, playerSixStat.text, nil];

I'm able to find the name of the player that scored the lowest with this:

int winner = [[scoreArray valueForKeyPath:@"@min.intValue"] intValue];
NSLog(@"the winner scored %d", winner);
NSString *string = [NSString stringWithFormat:@"%d", winner];
NSUInteger indexOfTheObject = [scoreArray indexOfObject:string];
NSString *playerString = [playerNameArray objectAtIndex:indexOfTheObject];
NSLog(@"The winner name is %@", playerString);

This works if there's only one player, but if there are two or more players that scored the same lowest score it can only display the name of the player that is first in the array order. How would I be able to modify the code to create a list/array of winners that share the same score?

Upvotes: 1

Views: 224

Answers (3)

iphondroid
iphondroid

Reputation: 500

Just add a for loop to your code. Other things are fine. Have not tested the code though. Check for formatting errors:

int winner = [[scoreArray valueForKeyPath:@"@min.intValue"] intValue];
NSLog(@"the winner scored %d", winner);
NSString *string = [NSString stringWithFormat:@"%d", winner];
for (int i=0; i<scoreArray.count; i++) {
    if ([scoreArray[i] isEqualToString:string]) {
        [indices addObject:@(i)];
    }
}
NSLog(@"%@",indices);
for (int i=0; i<indices.count; i++) {
    //print all player names with lowest score
    NSLog(@"The winner(s) are:");
    NSLog(@"%@,",playerNameArray[indices[i].intValue]);
}

Upvotes: 1

random
random

Reputation: 8608

I would do it using NSMutableDictionary instead of two arrays.

//NSMutableDictionary to store player scores rather than two arrays
NSMutableDictionary *players = [NSMutableDictionary new];

//Each player name is a key with their score stored as an NSNumber
[players setObject:@(10) forKey:@"John"];
[players setObject:@(15) forKey:@"Tim"];
[players setObject:@(20) forKey:@"Phil"];
[players setObject:@(17) forKey:@"Kurt"];
[players setObject:@(19) forKey:@"Cory"];
[players setObject:@(10) forKey:@"Robert"];

//Find the lowest score out of the bunch
NSNumber *theLowestScore = [[players allValues] valueForKeyPath:@"@min.intValue"];

//There could be multiple lowest score players so this will get everyone that is associated with the lowest score
NSArray *playersWithLowestScores = [players allKeysForObject:theLowestScore];

//Print out the players name who have the lowest score
[playersWithLowestScores enumerateObjectsUsingBlock:^(NSString *playerName, NSUInteger idx, BOOL *stop) {
    NSLog(@"%@", playerName);
}];

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726539

Since you have two "parallel" arrays, perhaps the simplest thing is to have a loop, like this:

for (int i = 0 ; i != scoreArray.count ; i++) {
    if (scoreArray[i].intValue == winner) {
         NSLog(@"Winner %@ at index %d.", playerNameArray[i], i);
    }
}

If you followed a more traditional approach and created an object representing a player, say,

@interface Player : NSObject
@property (nonatomic, readonly) NSString *name;
@property (nonatomic, readonly) int score;
-(id)initWithName:(NSString*)name andScore:(int)score;
@end

then you would be able to use a predicate to do a search, and retrieve all winners in a single shot.

Upvotes: 2

Related Questions