@@ -209,85 +209,85 @@ class CompanyStationsWindow : public Window
}

/** 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];
char buf[64];

SetDParam(0, (*a)->index);
SetDParam(0, a->index);
GetString(buf, STR_STATION_NAME, lastof(buf));

if (*b != last_station) {
last_station = *b;
SetDParam(0, (*b)->index);
if (b != last_station) {
last_station = b;
SetDParam(0, b->index);
GetString(buf_cache, STR_STATION_NAME, lastof(buf_cache));
}

int r = strnatcmp(buf, buf_cache); // Sort by name (natural sorting).
if (r == 0) return (*a)->index - (*b)->index;
return r;
if (r == 0) return a->index < b->index;
return r < 0;
}

/** 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 */
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;

CargoID j;
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 */
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;

CargoID j;
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 */
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 maxr2 = 0;

CargoID j;
FOR_EACH_SET_CARGO_ID(j, cargo_filter) {
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 (a->goods[j].HasRating()) maxr1 = max(maxr1, a->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 */
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 minr2 = 255;

for (CargoID j = 0; j < NUM_CARGO; j++) {
if (!HasBit(cargo_filter, j)) continue;
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 (a->goods[j].HasRating()) minr1 = min(minr1, a->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 */
@@ -69,9 +69,9 @@ struct StoryBookWindow : Window {
}

/** 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. */
@@ -98,9 +98,9 @@ struct StoryBookWindow : Window {
}

/** 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 @@ struct TownDirectoryWindow : public Window {
}

/** 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];
const Town *ta = *a;
const Town *tb = *b;
char buf[64];

SetDParam(0, ta->index);
SetDParam(0, a->index);
GetString(buf, STR_TOWN_NAME, lastof(buf));

/* 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
* times after each other) */
if (tb != last_town) {
last_town = tb;
SetDParam(0, tb->index);
if (b != last_town) {
last_town = b;
SetDParam(0, b->index);
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). */
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 b_population = (*b)->cache.population;
uint32 a_population = a->cache.population;
uint32 b_population = b->cache.population;
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 */
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. */
if (HasBit((*a)->have_ratings, _local_company)) {
if (HasBit((*b)->have_ratings, _local_company)) {
int16 a_rating = (*a)->ratings[_local_company];
int16 b_rating = (*b)->ratings[_local_company];
if (HasBit(a->have_ratings, _local_company)) {
if (HasBit(b->have_ratings, _local_company)) {
int16 a_rating = a->ratings[_local_company];
int16 b_rating = b->ratings[_local_company];
if (a_rating == b_rating) return TownDirectoryWindow::TownNameSorter(a, b);
return (a_rating < b_rating) ? -1 : 1;
return a_rating < b_rating;
}
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.
if (HasBit(b->have_ratings, _local_company)) return !before;

/* Sort unrated towns always on ascending town name. */
if (before) return TownDirectoryWindow::TownNameSorter(a, b);
return !TownDirectoryWindow::TownNameSorter(a, b);
}

public:
@@ -194,7 +194,7 @@ void BaseVehicleListWindow::SortVehicleList()
void DepotSortList(VehicleList *list)
{
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. */
@@ -1083,125 +1083,125 @@ StringID GetCargoSubtypeText(const Vehicle *v)
}

/** 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 */
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];

if (*a != _last_vehicle[0]) {
_last_vehicle[0] = *a;
SetDParam(0, (*a)->index);
if (a != _last_vehicle[0]) {
_last_vehicle[0] = a;
SetDParam(0, a->index);
GetString(last_name[0], STR_VEHICLE_NAME, lastof(last_name[0]));
}

if (*b != _last_vehicle[1]) {
_last_vehicle[1] = *b;
SetDParam(0, (*b)->index);
if (b != _last_vehicle[1]) {
_last_vehicle[1] = b;
SetDParam(0, b->index);
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).
return (r != 0) ? r : VehicleNumberSorter(a, b);
return (r != 0) ? r < 0: VehicleNumberSorter(a, b);
}

/** 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;
return (r != 0) ? r : VehicleNumberSorter(a, b);
int r = a->age - b->age;
return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
}

/** 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());
return (r != 0) ? r : VehicleNumberSorter(a, b);
int r = ClampToI32(a->GetDisplayProfitThisYear() - b->GetDisplayProfitThisYear());
return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
}

/** 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());
return (r != 0) ? r : VehicleNumberSorter(a, b);
int r = ClampToI32(a->GetDisplayProfitLastYear() - b->GetDisplayProfitLastYear());
return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
}

/** 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;
CargoArray diff;

/* Append the cargo of the connected waggons */
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 = 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;

int r = 0;
for (CargoID i = 0; i < NUM_CARGO; i++) {
r = diff[i];
if (r != 0) break;
}

return (r != 0) ? r : VehicleNumberSorter(a, b);
return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
}

/** 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;
return (r != 0) ? r : VehicleNumberSorter(a, b);
int r = a->reliability - b->reliability;
return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
}

/** 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;
return (r != 0) ? r : VehicleNumberSorter(a, b);
int r = a->vcache.cached_max_speed - b->vcache.cached_max_speed;
return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
}

/** 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;
return (r != 0) ? r : VehicleNumberSorter(a, b);
int r = a->engine_type - b->engine_type;
return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
}

/** 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;
Money diff = 0;

for (u = *a; u != nullptr; u = u->Next()) diff += u->value;
for (u = *b; 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;

int r = ClampToI32(diff);
return (r != 0) ? r : VehicleNumberSorter(a, b);
return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
}

/** 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;
return (r != 0) ? r : VehicleNumberSorter(a, b);
int r = a->GetGroundVehicleCache()->cached_total_length - b->GetGroundVehicleCache()->cached_total_length;
return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
}

/** 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));
return (r != 0) ? r : VehicleNumberSorter(a, b);
int r = ClampToI32((a->max_age - a->age) - (b->max_age - b->age));
return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
}

/** 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;
return (r != 0) ? r : VehicleNumberSorter(a, b);
int r = a->lateness_counter - b->lateness_counter;
return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
}

void InitializeGUI()