From de95d5f3417969af57449579ec60c70369a52655 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 18 Jul 2018 09:00:46 +0200 Subject: [PATCH] sorting sellers --- src/core/marketplace.cpp | 5 +++++ src/core/marketplace.h | 1 + src/core/seller.cpp | 8 +++++++- src/core/seller.h | 3 +++ src/gui/mainwindow.cpp | 1 + 5 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/core/marketplace.cpp b/src/core/marketplace.cpp index 14243f1..76efc43 100644 --- a/src/core/marketplace.cpp +++ b/src/core/marketplace.cpp @@ -41,4 +41,9 @@ int Marketplace::getNumSellersDelete() int count = std::count_if(sellers_.begin(), sellers_.end(), [](const auto& a) { return a->getState() == Seller::State::DELETE; }); return count; +} + +void Marketplace::sortSellers() +{ + std::sort(sellers_.begin(), sellers_.end()); } \ No newline at end of file diff --git a/src/core/marketplace.h b/src/core/marketplace.h index 580e12c..2eb6492 100644 --- a/src/core/marketplace.h +++ b/src/core/marketplace.h @@ -16,6 +16,7 @@ class Marketplace std::vector>& getSellers(); int getNextSellerNo(); int getNumSellersDelete(); + void sortSellers(); private: std::vector> sellers_; diff --git a/src/core/seller.cpp b/src/core/seller.cpp index def265e..3951b93 100644 --- a/src/core/seller.cpp +++ b/src/core/seller.cpp @@ -62,4 +62,10 @@ void Seller::cleanupArticles() } } -// int Seller::numArticlesTotal() const { return static_cast(getArticles().size()); } \ No newline at end of file +// int Seller::numArticlesTotal() const { return static_cast(getArticles().size()); } + +bool operator<(const Seller& li, const Seller& re) { return li.sellerNo_ < re.sellerNo_; } +bool operator<(const std::unique_ptr& li, const std::unique_ptr& re) +{ + return li->sellerNo_ < re->sellerNo_; +} \ No newline at end of file diff --git a/src/core/seller.h b/src/core/seller.h index e334fb2..dd5e7d2 100644 --- a/src/core/seller.h +++ b/src/core/seller.h @@ -32,6 +32,9 @@ class Seller : public Entity // int numArticlesTotal() const; std::vector getArticles(bool onlySold = true) const; + friend bool operator<(const Seller& li, const Seller& re); + friend bool operator<(const std::unique_ptr& li, const std::unique_ptr& re); + private: int sellerNo_{-1}; int numArticlesOffered_{}; diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 0839e78..a0ad153 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -22,6 +22,7 @@ void MainWindow::on_actionEditSeller_triggered() auto dialog = std::make_unique(this); int retCode = dialog->exec(); if (retCode == QDialog::Accepted) { + marketplace_->sortSellers(); marketplace_->storeToDb(); statusBar()->showMessage("Änderungen an den Verkäufer-Stammdaten gespeichert.", STATUSBAR_TIMEOUT);