I have a QList qList, I want to sort this based on the property "playerRank" in Players class. The Players class is as shown below.
class Players
{
public:
Players();
int playerId;
QString playerName;
int playerRank;
void setPlayerId(int id);
void setPlayerName(QString name);
void setPlayerRank(int rank);
};
#include "players.h"
Players::Players()
{
}
void Players::setPlayerId(int id)
{
playerId = id;
}
void Players::setPlayerName(QString name)
{
playerName = name;
}
void Players::setPlayerRank(int rank)
{
playerRank = rank;
}
How can I do this?
@Ishra's answer is technically correct, but we can do better.
const
so it works with containers to const objects toostd::sort(qList.begin(), qList.end(), [](const Players& p1, const Players& p2) {
return (p1.playerRank < p2.playerRank);
});