From e4f94747f33d1b5781e58555d8be954424fdeb35 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 14 Oct 2023 21:20:23 +0100 Subject: [PATCH] Codechange: Use comparator struct to sort cargo ID by predefined sort order. This allows reuse of the comparator where a typename is used instead. --- src/cargotype.h | 5 +++++ src/industry_gui.cpp | 8 ++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/cargotype.h b/src/cargotype.h index c1ec96882b..7b3674675f 100644 --- a/src/cargotype.h +++ b/src/cargotype.h @@ -204,4 +204,9 @@ static inline bool IsCargoInClass(CargoID c, CargoClass cc) using SetCargoBitIterator = SetBitIterator; +/** Comparator to sort CargoID by according to desired order. */ +struct CargoIDComparator { + bool operator() (const CargoID &lhs, const CargoID &rhs) const { return _sorted_cargo_types[lhs] < _sorted_cargo_types[rhs]; } +}; + #endif /* CARGOTYPE_H */ diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 931f714330..980222643b 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -1920,11 +1920,6 @@ enum CargoesFieldType { static const uint MAX_CARGOES = 16; ///< Maximum number of cargoes carried in a #CFT_CARGO field in #CargoesField. -static bool CargoIDSorter(const CargoID &a, const CargoID &b) -{ - return _sorted_cargo_types[a] < _sorted_cargo_types[b]; -} - /** Data about a single field in the #IndustryCargoesWindow panel. */ struct CargoesField { static int vert_inter_industry_space; @@ -2054,7 +2049,8 @@ struct CargoesField { } } this->u.cargo.num_cargoes = (count < 0) ? static_cast(insert - std::begin(this->u.cargo.vertical_cargoes)) : count; - std::sort(std::begin(this->u.cargo.vertical_cargoes), insert, &CargoIDSorter); + CargoIDComparator comparator; + std::sort(std::begin(this->u.cargo.vertical_cargoes), insert, comparator); std::fill(insert, std::end(this->u.cargo.vertical_cargoes), CT_INVALID); this->u.cargo.top_end = top_end; this->u.cargo.bottom_end = bottom_end;