I am performing a sort on the following collection of strings using QString::localeAwareCompare:
Search term used: "Mountain"
Results:
- Goblin Mountaineer
- Madblind Mountain
- Magnetic Mountain
- Mountain
- Mountain Goat
- Mountain Stronghold
- Mountain Titan
- Mountain Valley
- Mountain Yeti
- Snow-Covered Mountain
- The Lady of the Mountain
The order is sorted lexically according to QString::compare. Ultimately I would like the order to have the following rules:
- Exact match at top
- Exact match with preceding values sorted lexically.
Match contained in word sorted lexically
- Mountain (1)
- Mountain Goat (2)
- Mountain Stronghold (2)
- Mountain Titan (2)
- Mountain Valley (2)
- Mountain Yeti (2)
- Goblin Mountaineer (3)
- Madblind Mountain (3)
- Magnetic Mountain (3)
- Snow-Covered Mountain (3)
- The Lady of the Mountain (3)
Does anyone know how this might be achieved? I am able to implement a custom sort of some kind.
EDIT:
Here is some janky code I have tried to get exact matches to the top, which works.
bool CardDatabaseDisplayModel::lessThan(const QModelIndex &left, const QModelIndex &right) const {
QString leftString = sourceModel()->data(left).toString();
QString rightString = sourceModel()->data(right).toString();
if (leftString.compare(cardName, Qt::CaseInsensitive) == 0) {// exact match should be at top
return true;
}
if (rightString.compare(cardName, Qt::CaseInsensitive) == 0) {// exact match should be at top
return false;
}
return QString::localeAwareCompare(leftString, rightString) < 0;
}