Sorting Alphanumeric value list
-
Hi all,
I have a list in which the value can be alphanumeric, only numeric or only alphabets.
Which function is suitable to handle all these cases? I am using QSortFilterProxyModel.I tried using the following in lessThan() function.
- "leftData->primaryIdNo().compare(rightData->primaryIdNo(), sortCaseSensitivity()) < 0 ;"
Works fine for Strings not containing any digits. Numeric values are not sorted. - "leftData->primaryIdNo() < rightData->primaryIdNo();
Works for numeric values, but sorting of strings fails due to case sensitivity. For example "aaa" appears After "ZZZ".
How can I handle all these combinations?
- "leftData->primaryIdNo().compare(rightData->primaryIdNo(), sortCaseSensitivity()) < 0 ;"
-
Hi all,
I have a list in which the value can be alphanumeric, only numeric or only alphabets.
Which function is suitable to handle all these cases? I am using QSortFilterProxyModel.I tried using the following in lessThan() function.
- "leftData->primaryIdNo().compare(rightData->primaryIdNo(), sortCaseSensitivity()) < 0 ;"
Works fine for Strings not containing any digits. Numeric values are not sorted. - "leftData->primaryIdNo() < rightData->primaryIdNo();
Works for numeric values, but sorting of strings fails due to case sensitivity. For example "aaa" appears After "ZZZ".
How can I handle all these combinations?
@manny_lp
This question seems to just be aboutQSortFilterProxyModel
, nothing special about QML.As you say, you have two different ways of doing the comparison depending on whether the data is string or number. So you have to execute the correct one depending on whether the data you are looking at is alphabetic or numeric. The Qt model level does not know whether your data is to be treated as letters or numbers, only you do. So, depending, you might have something like:
bool MySortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const { QVariant &leftData( ... source_left ...); QVariant &rightData( ... source_right ...); if (isNumericColumn(source_left.column()) return leftData.toInt() < rightData.toInt(); else return (leftData.toString().compare(rightData.toString(), sortCaseSensitivity()) < 0); }
- "leftData->primaryIdNo().compare(rightData->primaryIdNo(), sortCaseSensitivity()) < 0 ;"
-
Solution is to use QCollator.
It goes something like this:
QCollator collator;
collator.setCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive);
collator.setNumericMode(true);
return collator.compare(leftData->primaryIdNo(),rightData->primaryIdNo()) < 0; -
Solution is to use QCollator.
It goes something like this:
QCollator collator;
collator.setCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive);
collator.setNumericMode(true);
return collator.compare(leftData->primaryIdNo(),rightData->primaryIdNo()) < 0;@manny_lp said in Sorting Alphanumeric value list:
collator.setNumericMode(true);
How would you know whether to set this
true
orfalse
? Seems to me to beg the question. Hence the need to know whether a give column is to be treated as numeric or alphabetic, as per my algorithm.QCollator
deals only in strings, won't work for e.g. dates or (I think) floating point numbers as a generic solution.