As yogeshgokul said the list is sorted well (lexicographically). The sorting you want you can achieve with custom comparator and use it with qSort():
struct MyLessThan {
QString st1
= s1.
left(s1.
indexOf('_'));
int it1 = s1.right(s1.size() - s1.indexOf('_') - 1).toInt();
QString st2
= s2.
left(s2.
indexOf('_'));
int it2 = s2.right(s2.size() - s2.indexOf('_') - 1).toInt();
if (st1 < st2)
return true;
if (st1 > st2)
return false;
if (it1 < it2)
return true;
return false;
}
};
struct MyLessThan {
bool operator()(const QString &s1, const QString &s2) const {
QString st1 = s1.left(s1.indexOf('_'));
int it1 = s1.right(s1.size() - s1.indexOf('_') - 1).toInt();
QString st2 = s2.left(s2.indexOf('_'));
int it2 = s2.right(s2.size() - s2.indexOf('_') - 1).toInt();
if (st1 < st2)
return true;
if (st1 > st2)
return false;
if (it1 < it2)
return true;
return false;
}
};
To copy to clipboard, switch view to plain text mode
than you can use it like this:
slist << "A_8" << "A_2" << "A_10" << "A_24" << "A_1";
MyLessThan le;
qSort(slist.begin(), slist.end(), le);
foreach
(const QString &str, slist
) { ui->plainTextEdit->appendPlainText(str);
}
QStringList slist;
slist << "A_8" << "A_2" << "A_10" << "A_24" << "A_1";
MyLessThan le;
qSort(slist.begin(), slist.end(), le);
foreach (const QString &str, slist) {
ui->plainTextEdit->appendPlainText(str);
}
To copy to clipboard, switch view to plain text mode
that gives me output:
Bookmarks