I'm using Qt and I created a ranking scene which contains all the players list with their score, so I have a QList<QString> list
, every QString contains a name and a score, separated by a space like this Mario 50
.
Now the problem is: how can I sort the list by the lowest score?
You can use std::sort()
with a lambda like this
std::sort(str_list.begin(), str_list.end(), [](const QString &lhs, const QString &rhs)
{
int num_lhs = lhs.split(' ').last().toInt();
int num_rhs = rhs.split(' ').last().toInt();
return num_lhs < num_rhs;
});
Thank you so much!!
If you find it helpful, please consider upvote and mark the answer correct.