From 88e588673b96485495c45a78787d796e74b524b4 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Mon, 31 Dec 2018 19:45:32 -0600 Subject: [PATCH] Qt Game List: Sort game titles "naturally". e.g. 10 comes after 9. --- .../Core/DolphinQt/GameList/GameListModel.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Source/Core/DolphinQt/GameList/GameListModel.cpp b/Source/Core/DolphinQt/GameList/GameListModel.cpp index 4644a08ae3..43bc61f9a1 100644 --- a/Source/Core/DolphinQt/GameList/GameListModel.cpp +++ b/Source/Core/DolphinQt/GameList/GameListModel.cpp @@ -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;