From 1e9e780776d0406a3eec5d98487fea20dc4f203b Mon Sep 17 00:00:00 2001 From: Isabell Pflug Date: Thu, 1 Jun 2023 08:39:17 +0200 Subject: [PATCH] :bug: Fixed highscore sorting --- ThinkPink/mainwindow.cpp | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/ThinkPink/mainwindow.cpp b/ThinkPink/mainwindow.cpp index ee15eac..44678cd 100644 --- a/ThinkPink/mainwindow.cpp +++ b/ThinkPink/mainwindow.cpp @@ -221,21 +221,33 @@ void MainWindow::showHighscore_triggered() { title->setAlignment(Qt::AlignCenter); //title->set layout->addWidget(title); - QList> sorted_keys = highscore.keys(); - std::sort(sorted_keys.begin(), sorted_keys.end()); - int i = 1; - for (auto key = sorted_keys.end()-1; key != sorted_keys.begin()-1; key--) { - auto entry = highscore.find(*key); + struct KeyValuePair { + QPair key; + int value; + }; + std::vector sorted_entries; + + for (auto entry = highscore.begin(); entry != highscore.end(); entry++) { + KeyValuePair pair; + pair.key = entry.key(); + pair.value = entry.value(); + sorted_entries.push_back(pair); + } + + std::sort(sorted_entries.begin(), sorted_entries.end(), [](const KeyValuePair& a, const KeyValuePair& b) { + return a.value > b.value; + }); + + for (unsigned long i = 1; i < sorted_entries.size()+1; i++) { QHBoxLayout *entry_layout = new QHBoxLayout; - QLabel *entry_name = new QLabel(QString::fromStdString(std::to_string(i)) + ") " + entry.key().first + ""); + QLabel *entry_name = new QLabel(QString::fromStdString(std::to_string(i)) + ") " + sorted_entries[i-1].key.first + ""); QLCDNumber *entry_score = new QLCDNumber(); - entry_score->display(entry.value()); - QLabel *entry_datetime = new QLabel(entry.key().second.toString(Qt::DateFormat::TextDate)); + entry_score->display( sorted_entries[i-1].value); + QLabel *entry_datetime = new QLabel( sorted_entries[i-1].key.second.toString(Qt::DateFormat::TextDate)); entry_layout->addWidget(entry_name); entry_layout->addWidget(entry_score); entry_layout->addWidget(entry_datetime); layout->addLayout(entry_layout); - i++; } highscoreDial.setLayout(layout); highscoreDial.exec();