From ca03d2741ee3c5976b548870f3a844c7fe62e85f Mon Sep 17 00:00:00 2001 From: Fedja Beader Date: Sun, 11 Feb 2024 03:54:47 +0000 Subject: Total weight sorting --- src/gui/widgets/itemcontainer.cpp | 54 ++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 9 deletions(-) (limited to 'src/gui/widgets') diff --git a/src/gui/widgets/itemcontainer.cpp b/src/gui/widgets/itemcontainer.cpp index a88c7798c..af1d24397 100644 --- a/src/gui/widgets/itemcontainer.cpp +++ b/src/gui/widgets/itemcontainer.cpp @@ -96,9 +96,9 @@ namespace if ((item1 == nullptr) || (item2 == nullptr)) return false; - const std::string name1 = item1->getInfo().getName( + const std::string& name1 = item1->getInfo().getName( item1->getColor()); - const std::string name2 = item2->getInfo().getName( + const std::string& name2 = item2->getInfo().getName( item2->getColor()); if (name1 == name2) { @@ -146,9 +146,9 @@ namespace const int w2 = pair2->mItem->getInfo().getWeight(); if (w1 == w2) { - const std::string name1 = + const std::string& name1 = pair1->mItem->getInfo().getName(); - const std::string name2 = + const std::string& name2 = pair2->mItem->getInfo().getName(); if (name1 == name2) { @@ -161,6 +161,38 @@ namespace } } itemWeightInvSorter; + class SortItemWeightTimesAmountFunctor final + { + public: + A_DEFAULT_COPY(SortItemWeightTimesAmountFunctor) + + bool operator() (const ItemIdPair *const pair1, + const ItemIdPair *const pair2) const + { + if ((pair1->mItem == nullptr) || (pair2->mItem == nullptr)) + return false; + + const int w1 = pair1->mItem->getInfo().getWeight() + * pair1->mItem->getQuantity(); + const int w2 = pair2->mItem->getInfo().getWeight() + * pair2->mItem->getQuantity(); + if (w1 == w2) + { + const std::string& name1 = + pair1->mItem->getInfo().getName(); + const std::string& name2 = + pair2->mItem->getInfo().getName(); + if (name1 == name2) + { + return pair1->mItem->getInvIndex() < + pair2->mItem->getInvIndex(); + } + return name1 < name2; + } + return w1 < w2; + } + } itemWeightTimesAmountInvSorter; + class SortItemAmountFunctor final { public: @@ -176,9 +208,9 @@ namespace const int c2 = pair2->mItem->getQuantity(); if (c1 == c2) { - const std::string name1 = + const std::string& name1 = pair1->mItem->getInfo().getName(); - const std::string name2 = + const std::string& name2 = pair2->mItem->getInfo().getName(); if (name1 == name2) { @@ -208,8 +240,8 @@ namespace const ItemDbTypeT t2 = info2.getType(); if (t1 == t2) { - const std::string &name1 = info1.getName(); - const std::string &name2 = info2.getName(); + const std::string& name1 = info1.getName(); + const std::string& name2 = info2.getName(); if (name1 == name2) { return pair1->mItem->getInvIndex() < @@ -1229,9 +1261,13 @@ int ItemContainer::updateMatrix() break; case 4: std::sort(sortedItems.begin(), sortedItems.end(), - itemAmountInvSorter); + itemWeightTimesAmountInvSorter); break; case 5: + std::sort(sortedItems.begin(), sortedItems.end(), + itemAmountInvSorter); + break; + case 6: std::sort(sortedItems.begin(), sortedItems.end(), itemTypeInvSorter); break; -- cgit v1.2.3-70-g09d2