🐛 Fixed highscore sorting

main
Isabell Pflug 2 years ago
parent 98cae7f1ab
commit 1e9e780776

@ -221,21 +221,33 @@ void MainWindow::showHighscore_triggered() {
title->setAlignment(Qt::AlignCenter); title->setAlignment(Qt::AlignCenter);
//title->set //title->set
layout->addWidget(title); layout->addWidget(title);
QList<QPair<QString, QDateTime>> sorted_keys = highscore.keys(); struct KeyValuePair {
std::sort(sorted_keys.begin(), sorted_keys.end()); QPair<QString, QDateTime> key;
int i = 1; int value;
for (auto key = sorted_keys.end()-1; key != sorted_keys.begin()-1; key--) { };
auto entry = highscore.find(*key); std::vector<KeyValuePair> 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; QHBoxLayout *entry_layout = new QHBoxLayout;
QLabel *entry_name = new QLabel(QString::fromStdString(std::to_string(i)) + ") <b>" + entry.key().first + "</b>"); QLabel *entry_name = new QLabel(QString::fromStdString(std::to_string(i)) + ") <b>" + sorted_entries[i-1].key.first + "</b>");
QLCDNumber *entry_score = new QLCDNumber(); QLCDNumber *entry_score = new QLCDNumber();
entry_score->display(entry.value()); entry_score->display( sorted_entries[i-1].value);
QLabel *entry_datetime = new QLabel(entry.key().second.toString(Qt::DateFormat::TextDate)); QLabel *entry_datetime = new QLabel( sorted_entries[i-1].key.second.toString(Qt::DateFormat::TextDate));
entry_layout->addWidget(entry_name); entry_layout->addWidget(entry_name);
entry_layout->addWidget(entry_score); entry_layout->addWidget(entry_score);
entry_layout->addWidget(entry_datetime); entry_layout->addWidget(entry_datetime);
layout->addLayout(entry_layout); layout->addLayout(entry_layout);
i++;
} }
highscoreDial.setLayout(layout); highscoreDial.setLayout(layout);
highscoreDial.exec(); highscoreDial.exec();

Loading…
Cancel
Save