Sorting
From qtnode
The header file <QtAlgorithms> offers a variety of useful template-based sorting functions. These functions can take iterators onto any Qt container class and sort it in place.
One of the more powerful features of the <QtAlgorithms> sorting functions is the ability to sort according to a user-specified callback function. Qt offers a templated qGreater function for reverse sorting and a caseInsensitiveLessThan for sorting strings in a case-insensitive manner. Qt does not, however, offer a large variety of these functions, so for many sorting tasks, a custom lessThan function must be written.
Sorting strings with embedded numbers
bool numericAwareLessThan(const QString& l, const QString& r) {
int len = l.length() < r.length() ? l.length() : r.length();
QString ls = l, rs = r;
QChar li, ri;
for(int i=0; i<len; i++) {
li = l.at(i);
ri = r.at(i);
if(li.isDigit() && ri.isDigit()) {
ls = l.mid(i);
rs = r.mid(i);
break;
} else {
if(li != ri) break;
}
}
double ld = ls.toDouble(), rd = rs.toDouble();
if(ld != rd)
return ld < rd;
else
return ls.localeAwareCompare(rs) < 0;
}