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!

Is it not working? It should work but I see you have the code commented out. Why is that? Here is a good reference for using sort.

When the code is uncommented it does not run due to...

  • 'Game::sortbyScore': function call missing argument list; use '&Game::sortbyScore' to create a pointer to member

  • 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided

From this i believe it's expecting some arguments for when i call sort but i'm don't believe it's necessary. Please correct me if i'm wrong as i'm not quite sure what arguments to pass.

It looks like your on the right track. I'm not going to bother putting it into my compiler to verify though. Is it working for you? Did you run into problems when you comment out line 26?

The program works perfectly when the calling of sort() is commented out. When it is uncommented the errors listed above occur

Game::sortbyScore

Wait, in your first post it was just sortbyScore. Try the suggestion in the error message, and reference it with &Game::sortbyScore as the third argument.

Edit: whoops forgot about the bool Game::sortbyScore(...)

bool Game::sortbyScore(const PlayerScore &ps1, const PlayerScore &ps2)
{
    return ps1.score > ps2.score;
}

I tried referencing with

sort(scores.begin(), scores.end(), &Game::sortbyScore);

and

    sort(scores.begin(), scores.end(), &sortbyScore);

if thats what you meant. Unfortunately, neither one solved the problem.

You need to declare the sortbyScore function as a static member of your class.. i.e., like so:

class Game {
  // ...

    static bool sortbyScore(const PlayerScore &ps1, const PlayerScore &ps2);
};

bool Game::sortbyScore(const PlayerScore &ps1, const PlayerScore &ps2)
{
    return ps1.score > ps2.score;
}

// call sort with:
sort(scores.begin(), scores.end(), &Game::sortbyScore);

Otherwise, you could also make the sortbyScore function into a free function, like so:

bool sortbyScore(const PlayerScore &ps1, const PlayerScore &ps2)
{
    return ps1.score > ps2.score;
}

// call sort with:
sort(scores.begin(), scores.end(), &sortbyScore);

Either way, that should work.

That did the job! I'll look up static to grasp a better understanding of it. Thank you to everyone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.