UndefinedDuck
UndefinedDuck

Reputation: 35

Sorting a QString list by numbers C++

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?

Upvotes: 1

Views: 1879

Answers (1)

Minh
Minh

Reputation: 1725

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;
});

Upvotes: 5

Related Questions