From ff5e8bb9a3fc5aa57f5277d56a49ff95e6bc3a75 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 31 Oct 2023 20:21:47 +0000 Subject: [PATCH] Fix #11413: Incorrect sorting by industry production. Error caused by single character mistake. However this algorithm was inefficent if a filter was specified, and clearly the flow was error-prone. Now using separately-scoped loops to avoid similar. --- src/industry_gui.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 4d808eecc9..824ce9b172 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -1509,14 +1509,16 @@ protected: if (filter == CF_NONE) return IndustryTypeSorter(a, b); uint prod_a = 0, prod_b = 0; - for (auto ita = std::begin(a->produced), itb = std::begin(b->produced); ita != std::end(a->produced) && itb != std::end(b->produced); ++ita, ++itb) { - if (filter == CF_ANY) { - if (IsValidCargoID(ita->cargo)) prod_a += ita->history[LAST_MONTH].production; - if (IsValidCargoID(itb->cargo)) prod_b += ita->history[LAST_MONTH].production; - } else { - if (ita->cargo == filter) prod_a += ita->history[LAST_MONTH].production; - if (itb->cargo == filter) prod_b += itb->history[LAST_MONTH].production; + if (filter == CF_ANY) { + for (const auto &pa : a->produced) { + if (IsValidCargoID(pa.cargo)) prod_a += pa.history[LAST_MONTH].production; } + for (const auto &pb : b->produced) { + if (IsValidCargoID(pb.cargo)) prod_b += pb.history[LAST_MONTH].production; + } + } else { + if (auto ita = a->GetCargoProduced(filter); ita != std::end(a->produced)) prod_a = ita->history[LAST_MONTH].production; + if (auto itb = b->GetCargoProduced(filter); itb != std::end(b->produced)) prod_b = itb->history[LAST_MONTH].production; } int r = prod_a - prod_b;