diff --git a/src/cargotype.cpp b/src/cargotype.cpp index f764ad2dd7..3b72e49647 100644 --- a/src/cargotype.cpp +++ b/src/cargotype.cpp @@ -142,6 +142,7 @@ SpriteID CargoSpec::GetCargoIcon() const return sprite; } +std::array _sorted_cargo_types; ///< Sort order of cargoes by cargo ID. std::vector _sorted_cargo_specs; ///< Cargo specifications sorted alphabetically by name. span _sorted_standard_cargo_specs; ///< Standard cargo specifications sorted alphabetically by name. @@ -186,6 +187,11 @@ void InitializeSortedCargoSpecs() /* Sort cargo specifications by cargo class and name. */ std::sort(_sorted_cargo_specs.begin(), _sorted_cargo_specs.end(), &CargoSpecClassSorter); + /* Populate */ + for (auto it = std::begin(_sorted_cargo_specs); it != std::end(_sorted_cargo_specs); ++it) { + _sorted_cargo_types[(*it)->Index()] = static_cast(it - std::begin(_sorted_cargo_specs)); + } + /* Count the number of standard cargos and fill the mask. */ _standard_cargo_mask = 0; uint8_t nb_standard_cargo = 0; diff --git a/src/cargotype.h b/src/cargotype.h index 8173a646d0..c1ec96882b 100644 --- a/src/cargotype.h +++ b/src/cargotype.h @@ -187,6 +187,7 @@ CargoID GetCargoIDByBitnum(uint8_t bitnum); CargoID GetDefaultCargoID(LandscapeID l, CargoType ct); void InitializeSortedCargoSpecs(); +extern std::array _sorted_cargo_types; extern std::vector _sorted_cargo_specs; extern span _sorted_standard_cargo_specs; diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 8b1e510b2d..354041b37a 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -1920,6 +1920,11 @@ 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; @@ -2049,6 +2054,7 @@ 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); 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; @@ -2813,7 +2819,7 @@ struct IndustryCargoesWindow : public Window { /* Add suppliers and customers of the 'it' industry. */ int supp_count = 0; int cust_count = 0; - for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) { + for (IndustryType it : _sorted_industry_types) { const IndustrySpec *indsp = GetIndustrySpec(it); if (!indsp->enabled) continue; @@ -2881,7 +2887,7 @@ struct IndustryCargoesWindow : public Window { /* Add suppliers and customers of the cargo. */ int supp_count = 0; int cust_count = 0; - for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) { + for (IndustryType it : _sorted_industry_types) { const IndustrySpec *indsp = GetIndustrySpec(it); if (!indsp->enabled) continue;