Skip to content
Permalink
Browse files
Merge pull request #7665 from jordan-woyak/game-list-natural-sort
Qt Game List: Sort game titles "naturally". e.g. 10 comes after 9.
  • Loading branch information
delroth committed Jan 1, 2019
2 parents 5c957ec + 88e5886 commit cddb83f
Showing 1 changed file with 17 additions and 0 deletions.
@@ -89,6 +89,8 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const
if (role == Qt::DisplayRole || role == Qt::InitialSortOrderRole)
{
QString name = QString::fromStdString(game.GetName(m_title_database));

// Add disc numbers > 1 to title if not present.
const int disc_nr = game.GetDiscNumber() + 1;
if (disc_nr > 1)
{
@@ -97,6 +99,21 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const
name.append(tr(" (Disc %1)").arg(disc_nr));
}
}

// For natural sorting, pad all numbers to the same length.
if (Qt::InitialSortOrderRole == role)
{
constexpr int MAX_NUMBER_LENGTH = 10;

QRegExp rx(QStringLiteral("\\d+"));
int pos = 0;
while ((pos = rx.indexIn(name, pos)) != -1)
{
name.replace(pos, rx.matchedLength(), rx.cap().rightJustified(MAX_NUMBER_LENGTH));
pos += MAX_NUMBER_LENGTH;
}
}

return name;
}
break;

0 comments on commit cddb83f

Please sign in to comment.