Hey Guys,
Was wondering if anyone could help me with this sort function for a vector of struct type. What i'm aiming to do is to make a score board being based on a player's score and print out the scores (along with the name) in descending order. I've looked around the net and i believe i could use a comparison function similar to...
bool sortbyScore(const PlayerScore &ps1, const PlayerScore &ps2)
{
return ps1.score > ps2.score;
}
and call it right before i print the vector.
struct PlayerScore {
string name;
int score;
} noah, bam;
vector<PlayerScore> scores;
void Game::setup()
{
noah.name = "Noah";
noah.score = 300;
bam.name = "Bam";
bam.score = 1000;
scores.push_back(noah);
scores.push_back(bam);
}
void Game::Leader()
{
int temp = 1;
//sort(scores.begin(), scores.end(), sortbyScore);
for (vector<PlayerScore>::iterator it = scores.begin(); it != scores.end(); ++it)
{
cout << temp << ". " << it->name << " - " << it->score << "\n";
temp++;
}
cout << "\n";
}
I'm not sure if i'm on the right track but any assistance will be appreciated!