Codechange: use std::sort() in GUIList

pull/88/head
glx 5 years ago committed by PeterN
parent b9b34f676b
commit 2db88953e7

@ -93,21 +93,21 @@ private:
Scrollbar *vscroll; Scrollbar *vscroll;
/** Sort the bridges by their index */ /** Sort the bridges by their index */
static int CDECL BridgeIndexSorter(const BuildBridgeData *a, const BuildBridgeData *b) static bool BridgeIndexSorter(const BuildBridgeData &a, const BuildBridgeData &b)
{ {
return a->index - b->index; return a.index < b.index;
} }
/** Sort the bridges by their price */ /** Sort the bridges by their price */
static int CDECL BridgePriceSorter(const BuildBridgeData *a, const BuildBridgeData *b) static bool BridgePriceSorter(const BuildBridgeData &a, const BuildBridgeData &b)
{ {
return a->cost - b->cost; return a.cost < b.cost;
} }
/** Sort the bridges by their maximum speed */ /** Sort the bridges by their maximum speed */
static int CDECL BridgeSpeedSorter(const BuildBridgeData *a, const BuildBridgeData *b) static bool BridgeSpeedSorter(const BuildBridgeData &a, const BuildBridgeData &b)
{ {
return a->spec->speed - b->spec->speed; return a.spec->speed < b.spec->speed;
} }
void BuildBridge(uint8 i) void BuildBridge(uint8 i)

@ -616,26 +616,26 @@ private:
ShowDropDownList(this, std::move(list), sel, widget); ShowDropDownList(this, std::move(list), sel, widget);
} }
static int CDECL GroupNameSorter(const Group * const *a, const Group * const *b) static bool GroupNameSorter(const Group * const &a, const Group * const &b)
{ {
static const Group *last_group[2] = { nullptr, nullptr }; static const Group *last_group[2] = { nullptr, nullptr };
static char last_name[2][64] = { "", "" }; static char last_name[2][64] = { "", "" };
if (*a != last_group[0]) { if (a != last_group[0]) {
last_group[0] = *a; last_group[0] = a;
SetDParam(0, (*a)->index); SetDParam(0, a->index);
GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0])); GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
} }
if (*b != last_group[1]) { if (b != last_group[1]) {
last_group[1] = *b; last_group[1] = b;
SetDParam(0, (*b)->index); SetDParam(0, b->index);
GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1])); GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
} }
int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting). int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
if (r == 0) return (*a)->index - (*b)->index; if (r == 0) return a->index < b->index;
return r; return r < 0;
} }
void AddChildren(GUIGroupList *source, GroupID parent, int indent) void AddChildren(GUIGroupList *source, GroupID parent, int indent)

@ -1148,9 +1148,9 @@ private:
} }
/** Sort the company league by performance history */ /** Sort the company league by performance history */
static int CDECL PerformanceSorter(const Company * const *c1, const Company * const *c2) static bool PerformanceSorter(const Company * const &c1, const Company * const &c2)
{ {
return (*c2)->old_economy[0].performance_history - (*c1)->old_economy[0].performance_history; return c2->old_economy[0].performance_history < c1->old_economy[0].performance_history;
} }
public: public:

@ -144,26 +144,26 @@ private:
} }
/** Sort the groups by their name */ /** Sort the groups by their name */
static int CDECL GroupNameSorter(const Group * const *a, const Group * const *b) static bool GroupNameSorter(const Group * const &a, const Group * const &b)
{ {
static const Group *last_group[2] = { nullptr, nullptr }; static const Group *last_group[2] = { nullptr, nullptr };
static char last_name[2][64] = { "", "" }; static char last_name[2][64] = { "", "" };
if (*a != last_group[0]) { if (a != last_group[0]) {
last_group[0] = *a; last_group[0] = a;
SetDParam(0, (*a)->index); SetDParam(0, a->index);
GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0])); GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
} }
if (*b != last_group[1]) { if (b != last_group[1]) {
last_group[1] = *b; last_group[1] = b;
SetDParam(0, (*b)->index); SetDParam(0, b->index);
GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1])); GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
} }
int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting). int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
if (r == 0) return (*a)->index - (*b)->index; if (r == 0) return a->index < b->index;
return r; return r < 0;
} }
/** /**

@ -1252,52 +1252,52 @@ protected:
} }
/** Sort industries by name */ /** Sort industries by name */
static int CDECL IndustryNameSorter(const Industry * const *a, const Industry * const *b) static bool IndustryNameSorter(const Industry * const &a, const Industry * const &b)
{ {
static char buf_cache[96]; static char buf_cache[96];
static char buf[96]; static char buf[96];
SetDParam(0, (*a)->index); SetDParam(0, a->index);
GetString(buf, STR_INDUSTRY_NAME, lastof(buf)); GetString(buf, STR_INDUSTRY_NAME, lastof(buf));
if (*b != last_industry) { if (b != last_industry) {
last_industry = *b; last_industry = b;
SetDParam(0, (*b)->index); SetDParam(0, b->index);
GetString(buf_cache, STR_INDUSTRY_NAME, lastof(buf_cache)); GetString(buf_cache, STR_INDUSTRY_NAME, lastof(buf_cache));
} }
return strnatcmp(buf, buf_cache); // Sort by name (natural sorting). return strnatcmp(buf, buf_cache) < 0; // Sort by name (natural sorting).
} }
/** Sort industries by type and name */ /** Sort industries by type and name */
static int CDECL IndustryTypeSorter(const Industry * const *a, const Industry * const *b) static bool IndustryTypeSorter(const Industry * const &a, const Industry * const &b)
{ {
int it_a = 0; int it_a = 0;
while (it_a != NUM_INDUSTRYTYPES && (*a)->type != _sorted_industry_types[it_a]) it_a++; while (it_a != NUM_INDUSTRYTYPES && a->type != _sorted_industry_types[it_a]) it_a++;
int it_b = 0; int it_b = 0;
while (it_b != NUM_INDUSTRYTYPES && (*b)->type != _sorted_industry_types[it_b]) it_b++; while (it_b != NUM_INDUSTRYTYPES && b->type != _sorted_industry_types[it_b]) it_b++;
int r = it_a - it_b; int r = it_a - it_b;
return (r == 0) ? IndustryNameSorter(a, b) : r; return (r == 0) ? IndustryNameSorter(a, b) : r < 0;
} }
/** Sort industries by production and name */ /** Sort industries by production and name */
static int CDECL IndustryProductionSorter(const Industry * const *a, const Industry * const *b) static bool IndustryProductionSorter(const Industry * const &a, const Industry * const &b)
{ {
uint prod_a = 0, prod_b = 0; uint prod_a = 0, prod_b = 0;
for (uint i = 0; i < lengthof((*a)->produced_cargo); i++) { for (uint i = 0; i < lengthof(a->produced_cargo); i++) {
if ((*a)->produced_cargo[i] != CT_INVALID) prod_a += (*a)->last_month_production[i]; if (a->produced_cargo[i] != CT_INVALID) prod_a += a->last_month_production[i];
if ((*b)->produced_cargo[i] != CT_INVALID) prod_b += (*b)->last_month_production[i]; if (b->produced_cargo[i] != CT_INVALID) prod_b += b->last_month_production[i];
} }
int r = prod_a - prod_b; int r = prod_a - prod_b;
return (r == 0) ? IndustryTypeSorter(a, b) : r; return (r == 0) ? IndustryTypeSorter(a, b) : r < 0;
} }
/** Sort industries by transported cargo and name */ /** Sort industries by transported cargo and name */
static int CDECL IndustryTransportedCargoSorter(const Industry * const *a, const Industry * const *b) static bool IndustryTransportedCargoSorter(const Industry * const &a, const Industry * const &b)
{ {
int r = GetCargoTransportedSortValue(*a) - GetCargoTransportedSortValue(*b); int r = GetCargoTransportedSortValue(a) - GetCargoTransportedSortValue(b);
return (r == 0) ? IndustryNameSorter(a, b) : r; return (r == 0) ? IndustryNameSorter(a, b) : r < 0;
} }
/** /**

@ -405,28 +405,28 @@ class NetworkContentListWindow : public Window, ContentCallback {
} }
/** Sort content by name. */ /** Sort content by name. */
static int CDECL NameSorter(const ContentInfo * const *a, const ContentInfo * const *b) static bool NameSorter(const ContentInfo * const &a, const ContentInfo * const &b)
{ {
return strnatcmp((*a)->name, (*b)->name, true); // Sort by name (natural sorting). return strnatcmp(a->name, b->name, true) < 0; // Sort by name (natural sorting).
} }
/** Sort content by type. */ /** Sort content by type. */
static int CDECL TypeSorter(const ContentInfo * const *a, const ContentInfo * const *b) static bool TypeSorter(const ContentInfo * const &a, const ContentInfo * const &b)
{ {
int r = 0; int r = 0;
if ((*a)->type != (*b)->type) { if (a->type != b->type) {
r = strnatcmp(content_type_strs[(*a)->type], content_type_strs[(*b)->type]); r = strnatcmp(content_type_strs[a->type], content_type_strs[b->type]);
} }
if (r == 0) r = NameSorter(a, b); if (r == 0) return NameSorter(a, b);
return r; return r < 0;
} }
/** Sort content by state. */ /** Sort content by state. */
static int CDECL StateSorter(const ContentInfo * const *a, const ContentInfo * const *b) static bool StateSorter(const ContentInfo * const &a, const ContentInfo * const &b)
{ {
int r = (*a)->state - (*b)->state; int r = a->state - b->state;
if (r == 0) r = TypeSorter(a, b); if (r == 0) return TypeSorter(a, b);
return r; return r < 0;
} }
/** Sort the content list */ /** Sort the content list */

@ -277,10 +277,10 @@ protected:
} }
/** Sort servers by name. */ /** Sort servers by name. */
static int CDECL NGameNameSorter(NetworkGameList * const *a, NetworkGameList * const *b) static bool NGameNameSorter(NetworkGameList * const &a, NetworkGameList * const &b)
{ {
int r = strnatcmp((*a)->info.server_name, (*b)->info.server_name, true); // Sort by name (natural sorting). int r = strnatcmp(a->info.server_name, b->info.server_name, true); // Sort by name (natural sorting).
return r == 0 ? (*a)->address.CompareTo((*b)->address) : r; return r == 0 ? a->address.CompareTo(b->address) < 0: r < 0;
} }
/** /**
@ -288,60 +288,60 @@ protected:
* server. If the two servers have the same amount, the one with the * server. If the two servers have the same amount, the one with the
* higher maximum is preferred. * higher maximum is preferred.
*/ */
static int CDECL NGameClientSorter(NetworkGameList * const *a, NetworkGameList * const *b) static bool NGameClientSorter(NetworkGameList * const &a, NetworkGameList * const &b)
{ {
/* Reverse as per default we are interested in most-clients first */ /* Reverse as per default we are interested in most-clients first */
int r = (*a)->info.clients_on - (*b)->info.clients_on; int r = a->info.clients_on - b->info.clients_on;
if (r == 0) r = (*a)->info.clients_max - (*b)->info.clients_max; if (r == 0) r = a->info.clients_max - b->info.clients_max;
if (r == 0) r = NGameNameSorter(a, b); if (r == 0) return NGameNameSorter(a, b);
return r; return r < 0;
} }
/** Sort servers by map size */ /** Sort servers by map size */
static int CDECL NGameMapSizeSorter(NetworkGameList * const *a, NetworkGameList * const *b) static bool NGameMapSizeSorter(NetworkGameList * const &a, NetworkGameList * const &b)
{ {
/* Sort by the area of the map. */ /* Sort by the area of the map. */
int r = ((*a)->info.map_height) * ((*a)->info.map_width) - ((*b)->info.map_height) * ((*b)->info.map_width); int r = (a->info.map_height) * (a->info.map_width) - (b->info.map_height) * (b->info.map_width);
if (r == 0) r = (*a)->info.map_width - (*b)->info.map_width; if (r == 0) r = a->info.map_width - b->info.map_width;
return (r != 0) ? r : NGameClientSorter(a, b); return (r != 0) ? r < 0 : NGameClientSorter(a, b);
} }
/** Sort servers by current date */ /** Sort servers by current date */
static int CDECL NGameDateSorter(NetworkGameList * const *a, NetworkGameList * const *b) static bool NGameDateSorter(NetworkGameList * const &a, NetworkGameList * const &b)
{ {
int r = (*a)->info.game_date - (*b)->info.game_date; int r = a->info.game_date - b->info.game_date;
return (r != 0) ? r : NGameClientSorter(a, b); return (r != 0) ? r < 0 : NGameClientSorter(a, b);
} }
/** Sort servers by the number of days the game is running */ /** Sort servers by the number of days the game is running */
static int CDECL NGameYearsSorter(NetworkGameList * const *a, NetworkGameList * const *b) static bool NGameYearsSorter(NetworkGameList * const &a, NetworkGameList * const &b)
{ {
int r = (*a)->info.game_date - (*a)->info.start_date - (*b)->info.game_date + (*b)->info.start_date; int r = a->info.game_date - a->info.start_date - b->info.game_date + b->info.start_date;
return (r != 0) ? r : NGameDateSorter(a, b); return (r != 0) ? r < 0: NGameDateSorter(a, b);
} }
/** /**
* Sort servers by joinability. If both servers are the * Sort servers by joinability. If both servers are the
* same, prefer the non-passworded server first. * same, prefer the non-passworded server first.
*/ */
static int CDECL NGameAllowedSorter(NetworkGameList * const *a, NetworkGameList * const *b) static bool NGameAllowedSorter(NetworkGameList * const &a, NetworkGameList * const &b)
{ {
/* The servers we do not know anything about (the ones that did not reply) should be at the bottom) */ /* The servers we do not know anything about (the ones that did not reply) should be at the bottom) */
int r = StrEmpty((*a)->info.server_revision) - StrEmpty((*b)->info.server_revision); int r = StrEmpty(a->info.server_revision) - StrEmpty(b->info.server_revision);
/* Reverse default as we are interested in version-compatible clients first */ /* Reverse default as we are interested in version-compatible clients first */
if (r == 0) r = (*b)->info.version_compatible - (*a)->info.version_compatible; if (r == 0) r = b->info.version_compatible - a->info.version_compatible;
/* The version-compatible ones are then sorted with NewGRF compatible first, incompatible last */ /* The version-compatible ones are then sorted with NewGRF compatible first, incompatible last */
if (r == 0) r = (*b)->info.compatible - (*a)->info.compatible; if (r == 0) r = b->info.compatible - a->info.compatible;
/* Passworded servers should be below unpassworded servers */ /* Passworded servers should be below unpassworded servers */
if (r == 0) r = (*a)->info.use_password - (*b)->info.use_password; if (r == 0) r = a->info.use_password - b->info.use_password;
/* Finally sort on the number of clients of the server */ /* Finally sort on the number of clients of the server */
if (r == 0) r = -NGameClientSorter(a, b); if (r == 0) return NGameClientSorter(a, b);
return r; return r < 0;
} }
/** Sort the server list */ /** Sort the server list */

@ -1426,15 +1426,15 @@ struct NewGRFWindow : public Window, NewGRFScanCallback {
private: private:
/** Sort grfs by name. */ /** Sort grfs by name. */
static int CDECL NameSorter(const GRFConfig * const *a, const GRFConfig * const *b) static bool NameSorter(const GRFConfig * const &a, const GRFConfig * const &b)
{ {
int i = strnatcmp((*a)->GetName(), (*b)->GetName(), true); // Sort by name (natural sorting). int i = strnatcmp(a->GetName(), b->GetName(), true); // Sort by name (natural sorting).
if (i != 0) return i; if (i != 0) return i < 0;
i = (*a)->version - (*b)->version; i = a->version - b->version;
if (i != 0) return i; if (i != 0) return i < 0;
return memcmp((*a)->ident.md5sum, (*b)->ident.md5sum, lengthof((*b)->ident.md5sum)); return memcmp(a->ident.md5sum, b->ident.md5sum, lengthof(b->ident.md5sum)) < 0;
} }
/** Filter grfs by tags/name */ /** Filter grfs by tags/name */

@ -72,21 +72,21 @@ struct SignList {
} }
/** Sort signs by their name */ /** Sort signs by their name */
static int CDECL SignNameSorter(const Sign * const *a, const Sign * const *b) static bool SignNameSorter(const Sign * const &a, const Sign * const &b)
{ {
/* Signs are very very rarely using the default text, but there can also be /* Signs are very very rarely using the default text, but there can also be
* a lot of them. Therefore a worthwhile performance gain can be made by * a lot of them. Therefore a worthwhile performance gain can be made by
* directly comparing Sign::name instead of going through the string * directly comparing Sign::name instead of going through the string
* system for each comparison. */ * system for each comparison. */
const char *a_name = (*a)->name; const char *a_name = a->name;
const char *b_name = (*b)->name; const char *b_name = b->name;
if (a_name == nullptr) a_name = SignList::default_name; if (a_name == nullptr) a_name = SignList::default_name;
if (b_name == nullptr) b_name = SignList::default_name; if (b_name == nullptr) b_name = SignList::default_name;
int r = strnatcmp(a_name, b_name); // Sort by name (natural sorting). int r = strnatcmp(a_name, b_name); // Sort by name (natural sorting).
return r != 0 ? r : ((*a)->index - (*b)->index); return r != 0 ? r < 0 : (a->index < b->index);
} }
void SortSignsList() void SortSignsList()

@ -49,8 +49,8 @@ struct Filtering {
template <typename T, typename F = const char*> template <typename T, typename F = const char*>
class GUIList : public std::vector<T> { class GUIList : public std::vector<T> {
public: public:
typedef int CDECL SortFunction(const T*, const T*); ///< Signature of sort function. typedef bool SortFunction(const T&, const T&); ///< Signature of sort function.
typedef bool CDECL FilterFunction(const T*, F); ///< Signature of filter function. typedef bool CDECL FilterFunction(const T*, F); ///< Signature of filter function.
protected: protected:
SortFunction * const *sort_func_list; ///< the sort criteria functions SortFunction * const *sort_func_list; ///< the sort criteria functions
@ -270,11 +270,11 @@ public:
if (this->flags & VL_FIRST_SORT) { if (this->flags & VL_FIRST_SORT) {
CLRBITS(this->flags, VL_FIRST_SORT); CLRBITS(this->flags, VL_FIRST_SORT);
QSortT(std::vector<T>::data(), std::vector<T>::size(), compare, desc); std::sort(std::vector<T>::begin(), std::vector<T>::end(), [&](const T &a, const T &b) { return desc ? compare(b, a) : compare(a, b); });
return true; return true;
} }
GSortT(std::vector<T>::data(), std::vector<T>::size(), compare, desc); std::sort(std::vector<T>::begin(), std::vector<T>::end(), [&](const T &a, const T &b) { return desc ? compare(b, a) : compare(a, b); });
return true; return true;
} }

@ -209,85 +209,85 @@ protected:
} }
/** Sort stations by their name */ /** Sort stations by their name */
static int CDECL StationNameSorter(const Station * const *a, const Station * const *b) static bool StationNameSorter(const Station * const &a, const Station * const &b)
{ {
static char buf_cache[64]; static char buf_cache[64];
char buf[64]; char buf[64];
SetDParam(0, (*a)->index); SetDParam(0, a->index);
GetString(buf, STR_STATION_NAME, lastof(buf)); GetString(buf, STR_STATION_NAME, lastof(buf));
if (*b != last_station) { if (b != last_station) {
last_station = *b; last_station = b;
SetDParam(0, (*b)->index); SetDParam(0, b->index);
GetString(buf_cache, STR_STATION_NAME, lastof(buf_cache)); GetString(buf_cache, STR_STATION_NAME, lastof(buf_cache));
} }
int r = strnatcmp(buf, buf_cache); // Sort by name (natural sorting). int r = strnatcmp(buf, buf_cache); // Sort by name (natural sorting).
if (r == 0) return (*a)->index - (*b)->index; if (r == 0) return a->index < b->index;
return r; return r < 0;
} }
/** Sort stations by their type */ /** Sort stations by their type */
static int CDECL StationTypeSorter(const Station * const *a, const Station * const *b) static bool StationTypeSorter(const Station * const &a, const Station * const &b)
{ {
return (*a)->facilities - (*b)->facilities; return a->facilities < b->facilities;
} }
/** Sort stations by their waiting cargo */ /** Sort stations by their waiting cargo */
static int CDECL StationWaitingTotalSorter(const Station * const *a, const Station * const *b) static bool StationWaitingTotalSorter(const Station * const &a, const Station * const &b)
{ {
int diff = 0; int diff = 0;
CargoID j; CargoID j;
FOR_EACH_SET_CARGO_ID(j, cargo_filter) { FOR_EACH_SET_CARGO_ID(j, cargo_filter) {
diff += (*a)->goods[j].cargo.TotalCount() - (*b)->goods[j].cargo.TotalCount(); diff += a->goods[j].cargo.TotalCount() - b->goods[j].cargo.TotalCount();
} }
return diff; return diff < 0;
} }
/** Sort stations by their available waiting cargo */ /** Sort stations by their available waiting cargo */
static int CDECL StationWaitingAvailableSorter(const Station * const *a, const Station * const *b) static bool StationWaitingAvailableSorter(const Station * const &a, const Station * const &b)
{ {
int diff = 0; int diff = 0;
CargoID j; CargoID j;
FOR_EACH_SET_CARGO_ID(j, cargo_filter) { FOR_EACH_SET_CARGO_ID(j, cargo_filter) {
diff += (*a)->goods[j].cargo.AvailableCount() - (*b)->goods[j].cargo.AvailableCount(); diff += a->goods[j].cargo.AvailableCount() - b->goods[j].cargo.AvailableCount();
} }
return diff; return diff < 0;
} }
/** Sort stations by their rating */ /** Sort stations by their rating */
static int CDECL StationRatingMaxSorter(const Station * const *a, const Station * const *b) static bool StationRatingMaxSorter(const Station * const &a, const Station * const &b)
{ {
byte maxr1 = 0; byte maxr1 = 0;
byte maxr2 = 0; byte maxr2 = 0;
CargoID j; CargoID j;
FOR_EACH_SET_CARGO_ID(j, cargo_filter) { FOR_EACH_SET_CARGO_ID(j, cargo_filter) {
if ((*a)->goods[j].HasRating()) maxr1 = max(maxr1, (*a)->goods[j].rating); if (a->goods[j].HasRating()) maxr1 = max(maxr1, a->goods[j].rating);
if ((*b)->goods[j].HasRating()) maxr2 = max(maxr2, (*b)->goods[j].rating); if (b->goods[j].HasRating()) maxr2 = max(maxr2, b->goods[j].rating);
} }
return maxr1 - maxr2; return maxr1 < maxr2;
} }
/** Sort stations by their rating */ /** Sort stations by their rating */
static int CDECL StationRatingMinSorter(const Station * const *a, const Station * const *b) static bool StationRatingMinSorter(const Station * const &a, const Station * const &b)
{ {
byte minr1 = 255; byte minr1 = 255;
byte minr2 = 255; byte minr2 = 255;
for (CargoID j = 0; j < NUM_CARGO; j++) { for (CargoID j = 0; j < NUM_CARGO; j++) {
if (!HasBit(cargo_filter, j)) continue; if (!HasBit(cargo_filter, j)) continue;
if ((*a)->goods[j].HasRating()) minr1 = min(minr1, (*a)->goods[j].rating); if (a->goods[j].HasRating()) minr1 = min(minr1, a->goods[j].rating);
if ((*b)->goods[j].HasRating()) minr2 = min(minr2, (*b)->goods[j].rating); if (b->goods[j].HasRating()) minr2 = min(minr2, b->goods[j].rating);
} }
return -(minr1 - minr2); return minr1 > minr2;
} }
/** Sort the stations list */ /** Sort the stations list */

@ -69,9 +69,9 @@ protected:
} }
/** Sort story pages by order value. */ /** Sort story pages by order value. */
static int CDECL PageOrderSorter(const StoryPage * const *a, const StoryPage * const *b) static bool PageOrderSorter(const StoryPage * const &a, const StoryPage * const &b)
{ {
return (*a)->sort_value - (*b)->sort_value; return a->sort_value < b->sort_value;
} }
/** (Re)Build story page element list. */ /** (Re)Build story page element list. */
@ -98,9 +98,9 @@ protected:
} }
/** Sort story page elements by order value. */ /** Sort story page elements by order value. */
static int CDECL PageElementOrderSorter(const StoryPageElement * const *a, const StoryPageElement * const *b) static bool PageElementOrderSorter(const StoryPageElement * const &a, const StoryPageElement * const &b)
{ {
return (*a)->sort_value - (*b)->sort_value; return a->sort_value < b->sort_value;
} }
/* /*

@ -668,54 +668,55 @@ private:
} }
/** Sort by town name */ /** Sort by town name */
static int CDECL TownNameSorter(const Town * const *a, const Town * const *b) static bool TownNameSorter(const Town * const &a, const Town * const &b)
{ {
static char buf_cache[64]; static char buf_cache[64];
const Town *ta = *a;
const Town *tb = *b;
char buf[64]; char buf[64];
SetDParam(0, ta->index); SetDParam(0, a->index);
GetString(buf, STR_TOWN_NAME, lastof(buf)); GetString(buf, STR_TOWN_NAME, lastof(buf));
/* If 'b' is the same town as in the last round, use the cached value /* If 'b' is the same town as in the last round, use the cached value
* We do this to speed stuff up ('b' is called with the same value a lot of * We do this to speed stuff up ('b' is called with the same value a lot of
* times after each other) */ * times after each other) */
if (tb != last_town) { if (b != last_town) {
last_town = tb; last_town = b;
SetDParam(0, tb->index); SetDParam(0, b->index);
GetString(buf_cache, STR_TOWN_NAME, lastof(buf_cache)); GetString(buf_cache, STR_TOWN_NAME, lastof(buf_cache));
} }
return strnatcmp(buf, buf_cache); // Sort by name (natural sorting). return strnatcmp(buf, buf_cache) < 0; // Sort by name (natural sorting).
} }
/** Sort by population (default descending, as big towns are of the most interest). */ /** Sort by population (default descending, as big towns are of the most interest). */
static int CDECL TownPopulationSorter(const Town * const *a, const Town * const *b) static bool TownPopulationSorter(const Town * const &a, const Town * const &b)
{ {
uint32 a_population = (*a)->cache.population; uint32 a_population = a->cache.population;
uint32 b_population = (*b)->cache.population; uint32 b_population = b->cache.population;
if (a_population == b_population) return TownDirectoryWindow::TownNameSorter(a, b); if (a_population == b_population) return TownDirectoryWindow::TownNameSorter(a, b);
return (a_population < b_population) ? -1 : 1; return a_population < b_population;
} }
/** Sort by town rating */ /** Sort by town rating */
static int CDECL TownRatingSorter(const Town * const *a, const Town * const *b) static bool TownRatingSorter(const Town * const &a, const Town * const &b)
{ {
int before = TownDirectoryWindow::last_sorting.order ? 1 : -1; // Value to get 'a' before 'b'. bool before = !TownDirectoryWindow::last_sorting.order; // Value to get 'a' before 'b'.
/* Towns without rating are always after towns with rating. */ /* Towns without rating are always after towns with rating. */
if (HasBit((*a)->have_ratings, _local_company)) { if (HasBit(a->have_ratings, _local_company)) {
if (HasBit((*b)->have_ratings, _local_company)) { if (HasBit(b->have_ratings, _local_company)) {
int16 a_rating = (*a)->ratings[_local_company]; int16 a_rating = a->ratings[_local_company];
int16 b_rating = (*b)->ratings[_local_company]; int16 b_rating = b->ratings[_local_company];
if (a_rating == b_rating) return TownDirectoryWindow::TownNameSorter(a, b); if (a_rating == b_rating) return TownDirectoryWindow::TownNameSorter(a, b);
return (a_rating < b_rating) ? -1 : 1; return a_rating < b_rating;
} }
return before; return before;
} }
if (HasBit((*b)->have_ratings, _local_company)) return -before; if (HasBit(b->have_ratings, _local_company)) return !before;
return -before * TownDirectoryWindow::TownNameSorter(a, b); // Sort unrated towns always on ascending town name.
/* Sort unrated towns always on ascending town name. */
if (before) return TownDirectoryWindow::TownNameSorter(a, b);
return !TownDirectoryWindow::TownNameSorter(a, b);
} }
public: public:

@ -194,7 +194,7 @@ void BaseVehicleListWindow::SortVehicleList()
void DepotSortList(VehicleList *list) void DepotSortList(VehicleList *list)
{ {
if (list->size() < 2) return; if (list->size() < 2) return;
QSortT(list->data(), list->size(), &VehicleNumberSorter); std::sort(list->begin(), list->end(), &VehicleNumberSorter);
} }
/** draw the vehicle profit button in the vehicle list window. */ /** draw the vehicle profit button in the vehicle list window. */
@ -1083,62 +1083,62 @@ StringID GetCargoSubtypeText(const Vehicle *v)
} }
/** Sort vehicles by their number */ /** Sort vehicles by their number */
static int CDECL VehicleNumberSorter(const Vehicle * const *a, const Vehicle * const *b) static bool VehicleNumberSorter(const Vehicle * const &a, const Vehicle * const &b)
{ {
return (*a)->unitnumber - (*b)->unitnumber; return a->unitnumber < b->unitnumber;
} }
/** Sort vehicles by their name */ /** Sort vehicles by their name */
static int CDECL VehicleNameSorter(const Vehicle * const *a, const Vehicle * const *b) static bool VehicleNameSorter(const Vehicle * const &a, const Vehicle * const &b)
{ {
static char last_name[2][64]; static char last_name[2][64];
if (*a != _last_vehicle[0]) { if (a != _last_vehicle[0]) {
_last_vehicle[0] = *a; _last_vehicle[0] = a;
SetDParam(0, (*a)->index); SetDParam(0, a->index);
GetString(last_name[0], STR_VEHICLE_NAME, lastof(last_name[0])); GetString(last_name[0], STR_VEHICLE_NAME, lastof(last_name[0]));
} }
if (*b != _last_vehicle[1]) { if (b != _last_vehicle[1]) {
_last_vehicle[1] = *b; _last_vehicle[1] = b;
SetDParam(0, (*b)->index); SetDParam(0, b->index);
GetString(last_name[1], STR_VEHICLE_NAME, lastof(last_name[1])); GetString(last_name[1], STR_VEHICLE_NAME, lastof(last_name[1]));
} }
int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting). int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
return (r != 0) ? r : VehicleNumberSorter(a, b); return (r != 0) ? r < 0: VehicleNumberSorter(a, b);
} }
/** Sort vehicles by their age */ /** Sort vehicles by their age */
static int CDECL VehicleAgeSorter(const Vehicle * const *a, const Vehicle * const *b) static bool VehicleAgeSorter(const Vehicle * const &a, const Vehicle * const &b)
{ {
int r = (*a)->age - (*b)->age; int r = a->age - b->age;
return (r != 0) ? r : VehicleNumberSorter(a, b); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
} }
/** Sort vehicles by this year profit */ /** Sort vehicles by this year profit */
static int CDECL VehicleProfitThisYearSorter(const Vehicle * const *a, const Vehicle * const *b) static bool VehicleProfitThisYearSorter(const Vehicle * const &a, const Vehicle * const &b)
{ {
int r = ClampToI32((*a)->GetDisplayProfitThisYear() - (*b)->GetDisplayProfitThisYear()); int r = ClampToI32(a->GetDisplayProfitThisYear() - b->GetDisplayProfitThisYear());
return (r != 0) ? r : VehicleNumberSorter(a, b); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
} }
/** Sort vehicles by last year profit */ /** Sort vehicles by last year profit */
static int CDECL VehicleProfitLastYearSorter(const Vehicle * const *a, const Vehicle * const *b) static bool VehicleProfitLastYearSorter(const Vehicle * const &a, const Vehicle * const &b)
{ {
int r = ClampToI32((*a)->GetDisplayProfitLastYear() - (*b)->GetDisplayProfitLastYear()); int r = ClampToI32(a->GetDisplayProfitLastYear() - b->GetDisplayProfitLastYear());
return (r != 0) ? r : VehicleNumberSorter(a, b); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
} }
/** Sort vehicles by their cargo */ /** Sort vehicles by their cargo */
static int CDECL VehicleCargoSorter(const Vehicle * const *a, const Vehicle * const *b) static bool VehicleCargoSorter(const Vehicle * const &a, const Vehicle * const &b)
{ {
const Vehicle *v; const Vehicle *v;
CargoArray diff; CargoArray diff;
/* Append the cargo of the connected waggons */ /* Append the cargo of the connected waggons */
for (v = *a; v != nullptr; v = v->Next()) diff[v->cargo_type] += v->cargo_cap; for (v = a; v != nullptr; v = v->Next()) diff[v->cargo_type] += v->cargo_cap;
for (v = *b; v != nullptr; v = v->Next()) diff[v->cargo_type] -= v->cargo_cap; for (v = b; v != nullptr; v = v->Next()) diff[v->cargo_type] -= v->cargo_cap;
int r = 0; int r = 0;
for (CargoID i = 0; i < NUM_CARGO; i++) { for (CargoID i = 0; i < NUM_CARGO; i++) {
@ -1146,62 +1146,62 @@ static int CDECL VehicleCargoSorter(const Vehicle * const *a, const Vehicle * co
if (r != 0) break; if (r != 0) break;
} }
return (r != 0) ? r : VehicleNumberSorter(a, b); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
} }
/** Sort vehicles by their reliability */ /** Sort vehicles by their reliability */
static int CDECL VehicleReliabilitySorter(const Vehicle * const *a, const Vehicle * const *b) static bool VehicleReliabilitySorter(const Vehicle * const &a, const Vehicle * const &b)
{ {
int r = (*a)->reliability - (*b)->reliability; int r = a->reliability - b->reliability;
return (r != 0) ? r : VehicleNumberSorter(a, b); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
} }
/** Sort vehicles by their max speed */ /** Sort vehicles by their max speed */
static int CDECL VehicleMaxSpeedSorter(const Vehicle * const *a, const Vehicle * const *b) static bool VehicleMaxSpeedSorter(const Vehicle * const &a, const Vehicle * const &b)
{ {
int r = (*a)->vcache.cached_max_speed - (*b)->vcache.cached_max_speed; int r = a->vcache.cached_max_speed - b->vcache.cached_max_speed;
return (r != 0) ? r : VehicleNumberSorter(a, b); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
} }
/** Sort vehicles by model */ /** Sort vehicles by model */
static int CDECL VehicleModelSorter(const Vehicle * const *a, const Vehicle * const *b) static bool VehicleModelSorter(const Vehicle * const &a, const Vehicle * const &b)
{ {
int r = (*a)->engine_type - (*b)->engine_type; int r = a->engine_type - b->engine_type;
return (r != 0) ? r : VehicleNumberSorter(a, b); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
} }
/** Sort vehicles by their value */ /** Sort vehicles by their value */
static int CDECL VehicleValueSorter(const Vehicle * const *a, const Vehicle * const *b) static bool VehicleValueSorter(const Vehicle * const &a, const Vehicle * const &b)
{ {
const Vehicle *u; const Vehicle *u;
Money diff = 0; Money diff = 0;
for (u = *a; u != nullptr; u = u->Next()) diff += u->value; for (u = a; u != nullptr; u = u->Next()) diff += u->value;
for (u = *b; u != nullptr; u = u->Next()) diff -= u->value; for (u = b; u != nullptr; u = u->Next()) diff -= u->value;
int r = ClampToI32(diff); int r = ClampToI32(diff);
return (r != 0) ? r : VehicleNumberSorter(a, b); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
} }
/** Sort vehicles by their length */ /** Sort vehicles by their length */
static int CDECL VehicleLengthSorter(const Vehicle * const *a, const Vehicle * const *b) static bool VehicleLengthSorter(const Vehicle * const &a, const Vehicle * const &b)
{ {
int r = (*a)->GetGroundVehicleCache()->cached_total_length - (*b)->GetGroundVehicleCache()->cached_total_length; int r = a->GetGroundVehicleCache()->cached_total_length - b->GetGroundVehicleCache()->cached_total_length;
return (r != 0) ? r : VehicleNumberSorter(a, b); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
} }
/** Sort vehicles by the time they can still live */ /** Sort vehicles by the time they can still live */
static int CDECL VehicleTimeToLiveSorter(const Vehicle * const *a, const Vehicle * const *b) static bool VehicleTimeToLiveSorter(const Vehicle * const &a, const Vehicle * const &b)
{ {
int r = ClampToI32(((*a)->max_age - (*a)->age) - ((*b)->max_age - (*b)->age)); int r = ClampToI32((a->max_age - a->age) - (b->max_age - b->age));
return (r != 0) ? r : VehicleNumberSorter(a, b); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
} }
/** Sort vehicles by the timetable delay */ /** Sort vehicles by the timetable delay */
static int CDECL VehicleTimetableDelaySorter(const Vehicle * const *a, const Vehicle * const *b) static bool VehicleTimetableDelaySorter(const Vehicle * const &a, const Vehicle * const &b)
{ {
int r = (*a)->lateness_counter - (*b)->lateness_counter; int r = a->lateness_counter - b->lateness_counter;
return (r != 0) ? r : VehicleNumberSorter(a, b); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
} }
void InitializeGUI() void InitializeGUI()

Loading…
Cancel
Save