Loading

convertsort.diff

  1. diff --git a/source.list b/source.list
  2. index df35cdd26..888578b4b 100644
  3. --- a/source.list
  4. +++ b/source.list
  5. @@ -442,7 +442,6 @@ core/smallmap_type.hpp
  6.  core/smallmatrix_type.hpp
  7.  core/smallstack_type.hpp
  8.  core/smallvec_type.hpp
  9. -core/sort_func.hpp
  10.  core/string_compare_type.hpp
  11.  
  12.  # GUI Source Code
  13. diff --git a/src/autoreplace_gui.cpp b/src/autoreplace_gui.cpp
  14. index 1074d1dd7..7342c1864 100644
  15. --- a/src/autoreplace_gui.cpp
  16. +++ b/src/autoreplace_gui.cpp
  17. @@ -32,11 +32,9 @@
  18.  
  19.  void DrawEngineList(VehicleType type, int x, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count, GroupID selected_group);
  20.  
  21. -static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b)
  22. +static bool CDECL EngineNumberSorter(const EngineID &a, const EngineID &b)
  23.  {
  24. -       int r = Engine::Get(*a)->list_position - Engine::Get(*b)->list_position;
  25. -
  26. -       return r;
  27. +       return Engine::Get(a)->list_position < Engine::Get(b)->list_position;
  28.  }
  29.  
  30.  /**
  31. diff --git a/src/bridge_gui.cpp b/src/bridge_gui.cpp
  32. index 797ead1f5..dfbe5d435 100644
  33. --- a/src/bridge_gui.cpp
  34. +++ b/src/bridge_gui.cpp
  35. @@ -93,21 +93,21 @@ private:
  36.         Scrollbar *vscroll;
  37.  
  38.         /** Sort the bridges by their index */
  39. -       static int CDECL BridgeIndexSorter(const BuildBridgeData *a, const BuildBridgeData *b)
  40. +       static bool CDECL BridgeIndexSorter(const BuildBridgeData &a, const BuildBridgeData &b)
  41.         {
  42. -               return a->index - b->index;
  43. +               return a.index < b.index;
  44.         }
  45.  
  46.         /** Sort the bridges by their price */
  47. -       static int CDECL BridgePriceSorter(const BuildBridgeData *a, const BuildBridgeData *b)
  48. +       static bool CDECL BridgePriceSorter(const BuildBridgeData &a, const BuildBridgeData &b)
  49.         {
  50. -               return a->cost - b->cost;
  51. +               return a.cost < b.cost;
  52.         }
  53.  
  54.         /** Sort the bridges by their maximum speed */
  55. -       static int CDECL BridgeSpeedSorter(const BuildBridgeData *a, const BuildBridgeData *b)
  56. +       static bool CDECL BridgeSpeedSorter(const BuildBridgeData &a, const BuildBridgeData &b)
  57.         {
  58. -               return a->spec->speed - b->spec->speed;
  59. +               return a.spec->speed < b.spec->speed;
  60.         }
  61.  
  62.         void BuildBridge(uint8 i)
  63. diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp
  64. index 655a18d7f..b7137d91d 100644
  65. --- a/src/build_vehicle_gui.cpp
  66. +++ b/src/build_vehicle_gui.cpp
  67. @@ -99,15 +99,14 @@ static CargoID _engine_sort_last_cargo_criteria[] = {CF_ANY, CF_ANY, CF_ANY, CF_
  68.  
  69.  /**
  70.   * Determines order of engines by engineID
  71. - * @param *a first engine to compare
  72. - * @param *b second engine to compare
  73. + * @param a first engine to compare
  74. + * @param b second engine to compare
  75.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  76.   */
  77. -static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b)
  78. +static bool CDECL EngineNumberSorter(const EngineID &a, const EngineID &b)
  79.  {
  80. -       int r = Engine::Get(*a)->list_position - Engine::Get(*b)->list_position;
  81. -
  82. -       return _engine_sort_direction ? -r : r;
  83. +       bool r = Engine::Get(a)->list_position < Engine::Get(b)->list_position;
  84. +       return _engine_sort_direction == r;
  85.  }
  86.  
  87.  /**
  88. @@ -116,15 +115,14 @@ static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b)
  89.   * @param *b second engine to compare
  90.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  91.   */
  92. -static int CDECL EngineIntroDateSorter(const EngineID *a, const EngineID *b)
  93. +static bool CDECL EngineIntroDateSorter(const EngineID &a, const EngineID &b)
  94.  {
  95. -       const int va = Engine::Get(*a)->intro_date;
  96. -       const int vb = Engine::Get(*b)->intro_date;
  97. -       const int r = va - vb;
  98. +       const int va = Engine::Get(a)->intro_date;
  99. +       const int vb = Engine::Get(b)->intro_date;
  100.  
  101.         /* Use EngineID to sort instead since we want consistent sorting */
  102. -       if (r == 0) return EngineNumberSorter(a, b);
  103. -       return _engine_sort_direction ? -r : r;
  104. +       if (va == vb) return EngineNumberSorter(a, b);
  105. +       return _engine_sort_direction == (va < vb);
  106.  }
  107.  
  108.  /**
  109. @@ -133,13 +131,13 @@ static int CDECL EngineIntroDateSorter(const EngineID *a, const EngineID *b)
  110.   * @param *b second engine to compare
  111.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  112.   */
  113. -static int CDECL EngineNameSorter(const EngineID *a, const EngineID *b)
  114. +static bool CDECL EngineNameSorter(const EngineID &a, const EngineID &b)
  115.  {
  116.         static EngineID last_engine[2] = { INVALID_ENGINE, INVALID_ENGINE };
  117.         static char     last_name[2][64] = { "\0", "\0" };
  118.  
  119. -       const EngineID va = *a;
  120. -       const EngineID vb = *b;
  121. +       const EngineID va = a;
  122. +       const EngineID vb = b;
  123. [20:40 lordaro@Apollo openttd] > vim build_vehicle_gui.cpp
  124. [20:40 lordaro@Apollo openttd] > vim src/build_vehicle_gui.cpp
  125. [20:42 lordaro@Apollo openttd] > make run
  126. make[1]: Entering directory '/home/lordaro/dev/openttd/objs/lang'
  127. make[1]: Nothing to be done for 'all'.
  128. make[1]: Leaving directory '/home/lordaro/dev/openttd/objs/lang'
  129. make[1]: Entering directory '/home/lordaro/dev/openttd/objs/setting'
  130. make[1]: Nothing to be done for 'all'.
  131. make[1]: Leaving directory '/home/lordaro/dev/openttd/objs/setting'
  132. make[1]: Entering directory '/home/lordaro/dev/openttd/objs/extra_grf'
  133. make[1]: Nothing to be done for 'all'.
  134. make[1]: Leaving directory '/home/lordaro/dev/openttd/objs/extra_grf'
  135. make[1]: Entering directory '/home/lordaro/dev/openttd/objs/release'
  136. [SRC] DEP CHECK (all files)
  137. [SRC] Compiling build_vehicle_gui.cpp
  138. [SRC] Linking openttd
  139. make[1]: Leaving directory '/home/lordaro/dev/openttd/objs/release'
  140. dbg: [grf] openttd_plus.0.1/ottdpp.grf: NewGRF "TD DP+ A set of trains designed to complement the default train set. This includes future maglevs and monorails as well as more default trains for desert/alpine." (GRFID 44460000) uses GRF version 1, which is incompatible with this version of OpenTTD.
  141. ALSA lib pcm.c:8323:(snd_pcm_recover) underrun occurred
  142. [20:44 lordaro@Apollo openttd] > git d
  143. diff --git a/source.list b/source.list
  144. index df35cdd26..888578b4b 100644
  145. --- a/source.list
  146. +++ b/source.list
  147. @@ -442,7 +442,6 @@ core/smallmap_type.hpp
  148.  core/smallmatrix_type.hpp
  149.  core/smallstack_type.hpp
  150.  core/smallvec_type.hpp
  151. -core/sort_func.hpp
  152.  core/string_compare_type.hpp
  153.  
  154.  # GUI Source Code
  155. diff --git a/src/autoreplace_gui.cpp b/src/autoreplace_gui.cpp
  156. index 1074d1dd7..7342c1864 100644
  157. --- a/src/autoreplace_gui.cpp
  158. +++ b/src/autoreplace_gui.cpp
  159. @@ -32,11 +32,9 @@
  160.  
  161.  void DrawEngineList(VehicleType type, int x, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count, GroupID selected_group);
  162.  
  163. -static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b)
  164. +static bool CDECL EngineNumberSorter(const EngineID &a, const EngineID &b)
  165.  {
  166. -       int r = Engine::Get(*a)->list_position - Engine::Get(*b)->list_position;
  167. -
  168. -       return r;
  169. +       return Engine::Get(a)->list_position < Engine::Get(b)->list_position;
  170.  }
  171.  
  172.  /**
  173. diff --git a/src/bridge_gui.cpp b/src/bridge_gui.cpp
  174. index 797ead1f5..dfbe5d435 100644
  175. --- a/src/bridge_gui.cpp
  176. +++ b/src/bridge_gui.cpp
  177. @@ -93,21 +93,21 @@ private:
  178.         Scrollbar *vscroll;
  179.  
  180.         /** Sort the bridges by their index */
  181. -       static int CDECL BridgeIndexSorter(const BuildBridgeData *a, const BuildBridgeData *b)
  182. +       static bool CDECL BridgeIndexSorter(const BuildBridgeData &a, const BuildBridgeData &b)
  183.         {
  184. -               return a->index - b->index;
  185. +               return a.index < b.index;
  186.         }
  187.  
  188.         /** Sort the bridges by their price */
  189. -       static int CDECL BridgePriceSorter(const BuildBridgeData *a, const BuildBridgeData *b)
  190. +       static bool CDECL BridgePriceSorter(const BuildBridgeData &a, const BuildBridgeData &b)
  191.         {
  192. -               return a->cost - b->cost;
  193. +               return a.cost < b.cost;
  194.         }
  195.  
  196.         /** Sort the bridges by their maximum speed */
  197. -       static int CDECL BridgeSpeedSorter(const BuildBridgeData *a, const BuildBridgeData *b)
  198. +       static bool CDECL BridgeSpeedSorter(const BuildBridgeData &a, const BuildBridgeData &b)
  199.         {
  200. -               return a->spec->speed - b->spec->speed;
  201. +               return a.spec->speed < b.spec->speed;
  202.         }
  203.  
  204.         void BuildBridge(uint8 i)
  205. diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp
  206. index 655a18d7f..ab71593d0 100644
  207. --- a/src/build_vehicle_gui.cpp
  208. +++ b/src/build_vehicle_gui.cpp
  209. @@ -99,15 +99,14 @@ static CargoID _engine_sort_last_cargo_criteria[] = {CF_ANY, CF_ANY, CF_ANY, CF_
  210.  
  211.  /**
  212.   * Determines order of engines by engineID
  213. - * @param *a first engine to compare
  214. - * @param *b second engine to compare
  215. + * @param a first engine to compare
  216. + * @param b second engine to compare
  217.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  218.   */
  219. -static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b)
  220. +static bool CDECL EngineNumberSorter(const EngineID &a, const EngineID &b)
  221.  {
  222. -       int r = Engine::Get(*a)->list_position - Engine::Get(*b)->list_position;
  223. -
  224. -       return _engine_sort_direction ? -r : r;
  225. +       bool r = Engine::Get(a)->list_position < Engine::Get(b)->list_position;
  226. +       return _engine_sort_direction != r;
  227.  }
  228.  
  229.  /**
  230. @@ -116,15 +115,14 @@ static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b)
  231.   * @param *b second engine to compare
  232.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  233.   */
  234. -static int CDECL EngineIntroDateSorter(const EngineID *a, const EngineID *b)
  235. +static bool CDECL EngineIntroDateSorter(const EngineID &a, const EngineID &b)
  236.  {
  237. -       const int va = Engine::Get(*a)->intro_date;
  238. -       const int vb = Engine::Get(*b)->intro_date;
  239. -       const int r = va - vb;
  240. +       const int va = Engine::Get(a)->intro_date;
  241. +       const int vb = Engine::Get(b)->intro_date;
  242.  
  243.         /* Use EngineID to sort instead since we want consistent sorting */
  244. -       if (r == 0) return EngineNumberSorter(a, b);
  245. -       return _engine_sort_direction ? -r : r;
  246. +       if (va == vb) return EngineNumberSorter(a, b);
  247. +       return _engine_sort_direction != (va < vb);
  248.  }
  249.  
  250.  /**
  251. @@ -133,13 +131,13 @@ static int CDECL EngineIntroDateSorter(const EngineID *a, const EngineID *b)
  252.   * @param *b second engine to compare
  253.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  254.   */
  255. -static int CDECL EngineNameSorter(const EngineID *a, const EngineID *b)
  256. +static bool CDECL EngineNameSorter(const EngineID &a, const EngineID &b)
  257.  {
  258.         static EngineID last_engine[2] = { INVALID_ENGINE, INVALID_ENGINE };
  259.         static char     last_name[2][64] = { "\0", "\0" };
  260.  
  261. -       const EngineID va = *a;
  262. -       const EngineID vb = *b;
  263. +       const EngineID va = a;
  264. +       const EngineID vb = b;
  265.  
  266.         if (va != last_engine[0]) {
  267.                 last_engine[0] = va;
  268. @@ -157,7 +155,7 @@ static int CDECL EngineNameSorter(const EngineID *a, const EngineID *b)
  269.  
  270.         /* Use EngineID to sort instead since we want consistent sorting */
  271.         if (r == 0) return EngineNumberSorter(a, b);
  272. -       return _engine_sort_direction ? -r : r;
  273. +       return _engine_sort_direction != (r < 0);
  274.  }
  275.  
  276.  /**
  277. @@ -166,15 +164,14 @@ static int CDECL EngineNameSorter(const EngineID *a, const EngineID *b)
  278.   * @param *b second engine to compare
  279.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  280.   */
  281. -static int CDECL EngineReliabilitySorter(const EngineID *a, const EngineID *b)
  282. +static bool CDECL EngineReliabilitySorter(const EngineID &a, const EngineID &b)
  283.  {
  284. -       const int va = Engine::Get(*a)->reliability;
  285. -       const int vb = Engine::Get(*b)->reliability;
  286. -       const int r = va - vb;
  287. +       const int va = Engine::Get(a)->reliability;
  288. +       const int vb = Engine::Get(b)->reliability;
  289.  
  290.         /* Use EngineID to sort instead since we want consistent sorting */
  291. -       if (r == 0) return EngineNumberSorter(a, b);
  292. -       return _engine_sort_direction ? -r : r;
  293. +       if (va == vb) return EngineNumberSorter(a, b);
  294. +       return _engine_sort_direction != (va < vb);
  295.  }
  296.  
  297.  /**
  298. @@ -183,15 +180,15 @@ static int CDECL EngineReliabilitySorter(const EngineID *a, const EngineID *b)
  299.   * @param *b second engine to compare
  300.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  301.   */
  302. -static int CDECL EngineCostSorter(const EngineID *a, const EngineID *b)
  303. +static bool CDECL EngineCostSorter(const EngineID &a, const EngineID &b)
  304.  {
  305. -       Money va = Engine::Get(*a)->GetCost();
  306. -       Money vb = Engine::Get(*b)->GetCost();
  307. +       Money va = Engine::Get(a)->GetCost();
  308. +       Money vb = Engine::Get(b)->GetCost();
  309.         int r = ClampToI32(va - vb);
  310.  
  311.         /* Use EngineID to sort instead since we want consistent sorting */
  312.         if (r == 0) return EngineNumberSorter(a, b);
  313. -       return _engine_sort_direction ? -r : r;
  314. +       return _engine_sort_direction != (r < 0);
  315.  }
  316.  
  317.  /**
  318. @@ -200,15 +197,15 @@ static int CDECL EngineCostSorter(const EngineID *a, const EngineID *b)
  319.   * @param *b second engine to compare
  320.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  321.   */
  322. -static int CDECL EngineSpeedSorter(const EngineID *a, const EngineID *b)
  323. +static bool CDECL EngineSpeedSorter(const EngineID &a, const EngineID &b)
  324.  {
  325. -       int va = Engine::Get(*a)->GetDisplayMaxSpeed();
  326. -       int vb = Engine::Get(*b)->GetDisplayMaxSpeed();
  327. +       int va = Engine::Get(a)->GetDisplayMaxSpeed();
  328. +       int vb = Engine::Get(b)->GetDisplayMaxSpeed();
  329.         int r = va - vb;
  330.  
  331.         /* Use EngineID to sort instead since we want consistent sorting */
  332.         if (r == 0) return EngineNumberSorter(a, b);
  333. -       return _engine_sort_direction ? -r : r;
  334. +       return _engine_sort_direction != (r < 0);
  335.  }
  336.  
  337.  /**
  338. @@ -217,15 +214,15 @@ static int CDECL EngineSpeedSorter(const EngineID *a, const EngineID *b)
  339.   * @param *b second engine to compare
  340.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  341.   */
  342. -static int CDECL EnginePowerSorter(const EngineID *a, const EngineID *b)
  343. +static bool CDECL EnginePowerSorter(const EngineID &a, const EngineID &b)
  344.  {
  345. -       int va = Engine::Get(*a)->GetPower();
  346. -       int vb = Engine::Get(*b)->GetPower();
  347. +       int va = Engine::Get(a)->GetPower();
  348. +       int vb = Engine::Get(b)->GetPower();
  349.         int r = va - vb;
  350.  
  351.         /* Use EngineID to sort instead since we want consistent sorting */
  352.         if (r == 0) return EngineNumberSorter(a, b);
  353. -       return _engine_sort_direction ? -r : r;
  354. +       return _engine_sort_direction != (r < 0);
  355.  }
  356.  
  357.  /**
  358. @@ -234,15 +231,15 @@ static int CDECL EnginePowerSorter(const EngineID *a, const EngineID *b)
  359.   * @param *b second engine to compare
  360.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  361.   */
  362. -static int CDECL EngineTractiveEffortSorter(const EngineID *a, const EngineID *b)
  363. +static bool CDECL EngineTractiveEffortSorter(const EngineID &a, const EngineID &b)
  364.  {
  365. -       int va = Engine::Get(*a)->GetDisplayMaxTractiveEffort();
  366. -       int vb = Engine::Get(*b)->GetDisplayMaxTractiveEffort();
  367. +       int va = Engine::Get(a)->GetDisplayMaxTractiveEffort();
  368. +       int vb = Engine::Get(b)->GetDisplayMaxTractiveEffort();
  369.         int r = va - vb;
  370.  
  371.         /* Use EngineID to sort instead since we want consistent sorting */
  372.         if (r == 0) return EngineNumberSorter(a, b);
  373. -       return _engine_sort_direction ? -r : r;
  374. +       return _engine_sort_direction != (r < 0);
  375.  }
  376.  
  377.  /**
  378. @@ -251,15 +248,15 @@ static int CDECL EngineTractiveEffortSorter(const EngineID *a, const EngineID *b
  379.   * @param *b second engine to compare
  380.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  381.   */
  382. -static int CDECL EngineRunningCostSorter(const EngineID *a, const EngineID *b)
  383. +static bool CDECL EngineRunningCostSorter(const EngineID &a, const EngineID &b)
  384.  {
  385. -       Money va = Engine::Get(*a)->GetRunningCost();
  386. -       Money vb = Engine::Get(*b)->GetRunningCost();
  387. +       Money va = Engine::Get(a)->GetRunningCost();
  388. +       Money vb = Engine::Get(b)->GetRunningCost();
  389.         int r = ClampToI32(va - vb);
  390.  
  391.         /* Use EngineID to sort instead since we want consistent sorting */
  392.         if (r == 0) return EngineNumberSorter(a, b);
  393. -       return _engine_sort_direction ? -r : r;
  394. +       return _engine_sort_direction != (r < 0);
  395.  }
  396.  
  397.  /**
  398. @@ -268,10 +265,10 @@ static int CDECL EngineRunningCostSorter(const EngineID *a, const EngineID *b)
  399.   * @param *b second engine to compare
  400.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  401.   */
  402. -static int CDECL EnginePowerVsRunningCostSorter(const EngineID *a, const EngineID *b)
  403. +static bool CDECL EnginePowerVsRunningCostSorter(const EngineID &a, const EngineID &b)
  404.  {
  405. -       const Engine *e_a = Engine::Get(*a);
  406. -       const Engine *e_b = Engine::Get(*b);
  407. +       const Engine *e_a = Engine::Get(a);
  408. +       const Engine *e_b = Engine::Get(b);
  409.  
  410.         /* Here we are using a few tricks to get the right sort.
  411.          * We want power/running cost, but since we usually got higher running cost than power and we store the result in an int,
  412. @@ -285,7 +282,7 @@ static int CDECL EnginePowerVsRunningCostSorter(const EngineID *a, const EngineI
  413.  
  414.         /* Use EngineID to sort instead since we want consistent sorting */
  415.         if (r == 0) return EngineNumberSorter(a, b);
  416. -       return _engine_sort_direction ? -r : r;
  417. +       return _engine_sort_direction != (r < 0);
  418.  }
  419.  
  420.  /* Train sorting functions */
  421. @@ -296,18 +293,18 @@ static int CDECL EnginePowerVsRunningCostSorter(const EngineID *a, const EngineI
  422.   * @param *b second engine to compare
  423.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  424.   */
  425. -static int CDECL TrainEngineCapacitySorter(const EngineID *a, const EngineID *b)
  426. +static bool CDECL TrainEngineCapacitySorter(const EngineID &a, const EngineID &b)
  427.  {
  428. -       const RailVehicleInfo *rvi_a = RailVehInfo(*a);
  429. -       const RailVehicleInfo *rvi_b = RailVehInfo(*b);
  430. +       const RailVehicleInfo *rvi_a = RailVehInfo(a);
  431. +       const RailVehicleInfo *rvi_b = RailVehInfo(b);
  432.  
  433. -       int va = GetTotalCapacityOfArticulatedParts(*a) * (rvi_a->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1);
  434. -       int vb = GetTotalCapacityOfArticulatedParts(*b) * (rvi_b->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1);
  435. +       int va = GetTotalCapacityOfArticulatedParts(a) * (rvi_a->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1);
  436. +       int vb = GetTotalCapacityOfArticulatedParts(b) * (rvi_b->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1);
  437.         int r = va - vb;
  438.  
  439.         /* Use EngineID to sort instead since we want consistent sorting */
  440.         if (r == 0) return EngineNumberSorter(a, b);
  441. -       return _engine_sort_direction ? -r : r;
  442. +       return _engine_sort_direction != (r < 0);
  443.  }
  444.  
  445.  /**
  446. @@ -316,15 +313,15 @@ static int CDECL TrainEngineCapacitySorter(const EngineID *a, const EngineID *b)
  447.   * @param *b second engine to compare
  448.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  449.   */
  450. -static int CDECL TrainEnginesThenWagonsSorter(const EngineID *a, const EngineID *b)
  451. +static bool CDECL TrainEnginesThenWagonsSorter(const EngineID &a, const EngineID &b)
  452.  {
  453. -       int val_a = (RailVehInfo(*a)->railveh_type == RAILVEH_WAGON ? 1 : 0);
  454. -       int val_b = (RailVehInfo(*b)->railveh_type == RAILVEH_WAGON ? 1 : 0);
  455. +       int val_a = (RailVehInfo(a)->railveh_type == RAILVEH_WAGON ? 1 : 0);
  456. +       int val_b = (RailVehInfo(b)->railveh_type == RAILVEH_WAGON ? 1 : 0);
  457.         int r = val_a - val_b;
  458.  
  459.         /* Use EngineID to sort instead since we want consistent sorting */
  460.         if (r == 0) return EngineNumberSorter(a, b);
  461. -       return _engine_sort_direction ? -r : r;
  462. +       return _engine_sort_direction != (r < 0);
  463.  }
  464.  
  465.  /* Road vehicle sorting functions */
  466. @@ -335,15 +332,15 @@ static int CDECL TrainEnginesThenWagonsSorter(const EngineID *a, const EngineID
  467.   * @param *b second engine to compare
  468.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  469.   */
  470. -static int CDECL RoadVehEngineCapacitySorter(const EngineID *a, const EngineID *b)
  471. +static bool CDECL RoadVehEngineCapacitySorter(const EngineID &a, const EngineID &b)
  472.  {
  473. -       int va = GetTotalCapacityOfArticulatedParts(*a);
  474. -       int vb = GetTotalCapacityOfArticulatedParts(*b);
  475. +       int va = GetTotalCapacityOfArticulatedParts(a);
  476. +       int vb = GetTotalCapacityOfArticulatedParts(b);
  477.         int r = va - vb;
  478.  
  479.         /* Use EngineID to sort instead since we want consistent sorting */
  480.         if (r == 0) return EngineNumberSorter(a, b);
  481. -       return _engine_sort_direction ? -r : r;
  482. +       return _engine_sort_direction != (r < 0);
  483.  }
  484.  
  485.  /* Ship vehicle sorting functions */
  486. @@ -354,10 +351,10 @@ static int CDECL RoadVehEngineCapacitySorter(const EngineID *a, const EngineID *
  487.   * @param *b second engine to compare
  488.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  489.   */
  490. -static int CDECL ShipEngineCapacitySorter(const EngineID *a, const EngineID *b)
  491. +static bool CDECL ShipEngineCapacitySorter(const EngineID &a, const EngineID &b)
  492.  {
  493. -       const Engine *e_a = Engine::Get(*a);
  494. -       const Engine *e_b = Engine::Get(*b);
  495. +       const Engine *e_a = Engine::Get(a);
  496. +       const Engine *e_b = Engine::Get(b);
  497.  
  498.         int va = e_a->GetDisplayDefaultCapacity();
  499.         int vb = e_b->GetDisplayDefaultCapacity();
  500. @@ -365,7 +362,7 @@ static int CDECL ShipEngineCapacitySorter(const EngineID *a, const EngineID *b)
  501.  
  502.         /* Use EngineID to sort instead since we want consistent sorting */
  503.         if (r == 0) return EngineNumberSorter(a, b);
  504. -       return _engine_sort_direction ? -r : r;
  505. +       return _engine_sort_direction != (r < 0);
  506.  }
  507.  
  508.  /* Aircraft sorting functions */
  509. @@ -376,10 +373,10 @@ static int CDECL ShipEngineCapacitySorter(const EngineID *a, const EngineID *b)
  510.   * @param *b second engine to compare
  511.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
  512.   */
  513. -static int CDECL AircraftEngineCargoSorter(const EngineID *a, const EngineID *b)
  514. +static bool CDECL AircraftEngineCargoSorter(const EngineID &a, const EngineID &b)
  515.  {
  516. -       const Engine *e_a = Engine::Get(*a);
  517. -       const Engine *e_b = Engine::Get(*b);
  518. +       const Engine *e_a = Engine::Get(a);
  519. +       const Engine *e_b = Engine::Get(b);
  520.  
  521.         uint16 mail_a, mail_b;
  522.         int va = e_a->GetDisplayDefaultCapacity(&mail_a);
  523. @@ -395,7 +392,7 @@ static int CDECL AircraftEngineCargoSorter(const EngineID *a, const EngineID *b)
  524.                         return EngineNumberSorter(a, b);
  525.                 }
  526.         }
  527. -       return _engine_sort_direction ? -r : r;
  528. +       return _engine_sort_direction != (r < 0);
  529.  }
  530.  
  531.  /**
  532. @@ -404,16 +401,16 @@ static int CDECL AircraftEngineCargoSorter(const EngineID *a, const EngineID *b)
  533.   * @param *b second engine to compare.
  534.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal.
  535.   */
  536. -static int CDECL AircraftRangeSorter(const EngineID *a, const EngineID *b)
  537. +static bool CDECL AircraftRangeSorter(const EngineID &a, const EngineID &b)
  538.  {
  539. -       uint16 r_a = Engine::Get(*a)->GetRange();
  540. -       uint16 r_b = Engine::Get(*b)->GetRange();
  541. +       uint16 r_a = Engine::Get(a)->GetRange();
  542. +       uint16 r_b = Engine::Get(b)->GetRange();
  543.  
  544.         int r = r_a - r_b;
  545.  
  546.         /* Use EngineID to sort instead since we want consistent sorting */
  547.         if (r == 0) return EngineNumberSorter(a, b);
  548. -       return _engine_sort_direction ? -r : r;
  549. +       return _engine_sort_direction != (r < 0);
  550.  }
  551.  
  552.  /** Sort functions for the vehicle sort criteria, for each vehicle type. */
  553. diff --git a/src/cargotype.cpp b/src/cargotype.cpp
  554. index 863c58561..ce7622798 100644
  555. --- a/src/cargotype.cpp
  556. +++ b/src/cargotype.cpp
  557. @@ -10,11 +10,13 @@
  558.  /** @file cargotype.cpp Implementation of cargoes. */
  559.  
  560.  #include "stdafx.h"
  561. +
  562. +#include <algorithm>
  563. +
  564.  #include "cargotype.h"
  565.  #include "newgrf_cargo.h"
  566.  #include "string_func.h"
  567.  #include "strings_func.h"
  568. -#include "core/sort_func.hpp"
  569.  
  570.  #include "table/sprites.h"
  571.  #include "table/strings.h"
  572. @@ -138,35 +140,28 @@ uint8 _sorted_standard_cargo_specs_size;         ///< Number of standard cargo s
  573.  
  574.  
  575.  /** Sort cargo specifications by their name. */
  576. -static int CDECL CargoSpecNameSorter(const CargoSpec * const *a, const CargoSpec * const *b)
  577. +static bool CDECL CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b)
  578.  {
  579.         static char a_name[64];
  580.         static char b_name[64];
  581.  
  582. -       GetString(a_name, (*a)->name, lastof(a_name));
  583. -       GetString(b_name, (*b)->name, lastof(b_name));
  584. +       GetString(a_name, a->name, lastof(a_name));
  585. +       GetString(b_name, b->name, lastof(b_name));
  586.  
  587.         int res = strnatcmp(a_name, b_name); // Sort by name (natural sorting).
  588.  
  589.         /* If the names are equal, sort by cargo bitnum. */
  590. -       return (res != 0) ? res : ((*a)->bitnum - (*b)->bitnum);
  591. +       return (res != 0) ? res < 0 : (a->bitnum < b->bitnum);
  592.  }
  593.  
  594.  /** Sort cargo specifications by their cargo class. */
  595. -static int CDECL CargoSpecClassSorter(const CargoSpec * const *a, const CargoSpec * const *b)
  596. +static bool CDECL CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * const &b)
  597.  {
  598.         uint16 mail_a, mail_b;
  599.         int va = e_a->GetDisplayDefaultCapacity(&mail_a);
  600. @@ -395,7 +392,7 @@ static int CDECL AircraftEngineCargoSorter(const EngineID *a, const EngineID *b)
  601.                         return EngineNumberSorter(a, b);
  602.                 }
  603.         }
  604. -       return _engine_sort_direction ? -r : r;
  605. +       return _engine_sort_direction != (r < 0);
  606.  }
  607.  
  608.  /**
  609. @@ -404,16 +401,16 @@ static int CDECL AircraftEngineCargoSorter(const EngineID *a, const EngineID *b)
  610.   * @param *b second engine to compare.
  611.   * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal.
  612.   */
  613. -static int CDECL AircraftRangeSorter(const EngineID *a, const EngineID *b)
  614. +static bool CDECL AircraftRangeSorter(const EngineID &a, const EngineID &b)
  615.  {
  616. -       uint16 r_a = Engine::Get(*a)->GetRange();
  617. -       uint16 r_b = Engine::Get(*b)->GetRange();
  618. +       uint16 r_a = Engine::Get(a)->GetRange();
  619. +       uint16 r_b = Engine::Get(b)->GetRange();
  620.  
  621.         int r = r_a - r_b;
  622.  
  623.         /* Use EngineID to sort instead since we want consistent sorting */
  624.         if (r == 0) return EngineNumberSorter(a, b);
  625. -       return _engine_sort_direction ? -r : r;
  626. +       return _engine_sort_direction != (r < 0);
  627.  }
  628.  
  629.  /** Sort functions for the vehicle sort criteria, for each vehicle type. */
  630. diff --git a/src/cargotype.cpp b/src/cargotype.cpp
  631. index 863c58561..ce7622798 100644
  632. --- a/src/cargotype.cpp
  633. +++ b/src/cargotype.cpp
  634. @@ -10,11 +10,13 @@
  635.  /** @file cargotype.cpp Implementation of cargoes. */
  636.  
  637.  #include "stdafx.h"
  638. +
  639. +#include <algorithm>
  640. +
  641.  #include "cargotype.h"
  642.  #include "newgrf_cargo.h"
  643.  #include "string_func.h"
  644.  #include "strings_func.h"
  645. -#include "core/sort_func.hpp"
  646.  
  647.  #include "table/sprites.h"
  648.  #include "table/strings.h"
  649. @@ -138,35 +140,28 @@ uint8 _sorted_standard_cargo_specs_size;         ///< Number of standard cargo s
  650.  
  651.  
  652.  /** Sort cargo specifications by their name. */
  653. -static int CDECL CargoSpecNameSorter(const CargoSpec * const *a, const CargoSpec * const *b)
  654. +static bool CDECL CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b)
  655.  {
  656.         static char a_name[64];
  657.         static char b_name[64];
  658.  
  659. -       GetString(a_name, (*a)->name, lastof(a_name));
  660. -       GetString(b_name, (*b)->name, lastof(b_name));
  661. +       GetString(a_name, a->name, lastof(a_name));
  662. +       GetString(b_name, b->name, lastof(b_name));
  663.  
  664.         int res = strnatcmp(a_name, b_name); // Sort by name (natural sorting).
  665.  
  666.         /* If the names are equal, sort by cargo bitnum. */
  667. -       return (res != 0) ? res : ((*a)->bitnum - (*b)->bitnum);
  668. +       return (res != 0) ? res < 0 : (a->bitnum < b->bitnum);
  669.  }
  670.  
  671.  /** Sort cargo specifications by their cargo class. */
  672. -static int CDECL CargoSpecClassSorter(const CargoSpec * const *a, const CargoSpec * const *b)
  673. +static bool CDECL CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * const &b)
  674.  {
  675. -       int res = ((*b)->classes & CC_PASSENGERS) - ((*a)->classes & CC_PASSENGERS);
  676. -       if (res == 0) {
  677. -               res = ((*b)->classes & CC_MAIL) - ((*a)->classes & CC_MAIL);
  678. -               if (res == 0) {
  679. -                       res = ((*a)->classes & CC_SPECIAL) - ((*b)->classes & CC_SPECIAL);
  680. -                       if (res == 0) {
  681. -                               return CargoSpecNameSorter(a, b);
  682. -                       }
  683. -               }
  684. -       }
  685. -
  686. -       return res;
  687. +       int r = (b->classes & CC_PASSENGERS) - (a->classes & CC_PASSENGERS);
  688. +       if (r == 0) r = (b->classes & CC_MAIL) - (a->classes & CC_MAIL);
  689. +       if (r == 0) r = (a->classes & CC_SPECIAL) - (b->classes & CC_SPECIAL);
  690. +       if (r == 0) return CargoSpecNameSorter(a, b);
  691. +       return r < 0;
  692.  }
  693.  
  694.  /** Initialize the list of sorted cargo specifications. */
  695. @@ -181,7 +176,7 @@ void InitializeSortedCargoSpecs()
  696.         }
  697.  
  698.         /* Sort cargo specifications by cargo class and name. */
  699. -       QSortT(_sorted_cargo_specs, _sorted_cargo_specs_size, &CargoSpecClassSorter);
  700. +       std::sort(_sorted_cargo_specs, _sorted_cargo_specs + _sorted_cargo_specs_size, &CargoSpecClassSorter);
  701.  
  702.         _standard_cargo_mask = 0;
  703.  
  704. diff --git a/src/core/smallmap_type.hpp b/src/core/smallmap_type.hpp
  705. index dda0fc2a1..49b5f3b34 100644
  706. --- a/src/core/smallmap_type.hpp
  707. +++ b/src/core/smallmap_type.hpp
  708. @@ -12,8 +12,9 @@
  709.  #ifndef SMALLMAP_TYPE_HPP
  710.  #define SMALLMAP_TYPE_HPP
  711.  
  712. +#include <algorithm>
  713. +
  714.  #include "smallvec_type.hpp"
  715. -#include "sort_func.hpp"
  716.  
  717.  /**
  718.   * Simple pair of data. Both types have to be POD ("Plain Old Data")!
  719. @@ -146,12 +147,12 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
  720.  
  721.         inline void SortByKey()
  722.         {
  723. -               QSortT(this->Begin(), this->items, KeySorter);
  724. +               std::sort(this->Begin(), this->End(), KeySorter);
  725.         }
  726.  
  727. -       static int CDECL KeySorter(const Pair *a, const Pair *b)
  728. +       static bool CDECL KeySorter(const Pair &a, const Pair &b)
  729.         {
  730. -               return a->first - b->first;
  731. +               return a->first < b->first;
  732.         }
  733.  };
  734.  
  735. diff --git a/src/core/sort_func.hpp b/src/core/sort_func.hpp
  736. deleted file mode 100644
  737. index 470a0ccf4..000000000
  738. --- a/src/core/sort_func.hpp
  739. +++ /dev/null
  740. @@ -1,89 +0,0 @@
  741. -/* $Id$ */
  742. -
  743. -/*
  744. - * This file is part of OpenTTD.
  745. - * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
  746. - * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  747. - * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
  748. - */
  749. -
  750. -/** @file sort_func.hpp Functions related to sorting operations. */
  751. -
  752. -#ifndef SORT_FUNC_HPP
  753. -#define SORT_FUNC_HPP
  754. -
  755. -#include "mem_func.hpp"
  756. -
  757. -/**
  758. - * Type safe qsort()
  759. - *
  760. - * @note Use this sort for irregular sorted data.
  761. - *
  762. - * @param base Pointer to the first element of the array to be sorted.
  763. - * @param num Number of elements in the array pointed by base.
  764. - * @param comparator Function that compares two elements.
  765. - * @param desc Sort descending.
  766. - */
  767. -template <typename T>
  768. -static inline void QSortT(T *base, uint num, int (CDECL *comparator)(const T*, const T*), bool desc = false)
  769. -{
  770. -       if (num < 2) return;
  771. -
  772. -       qsort(base, num, sizeof(T), (int (CDECL *)(const void *, const void *))comparator);
  773. -
  774. -       if (desc) MemReverseT(base, num);
  775. -}
  776. -
  777. -/**
  778. - * Type safe Gnome Sort.
  779. - *
  780. - * This is a slightly modified Gnome search. The basic
  781. - * Gnome search tries to sort already sorted list parts.
  782. - * The modification skips these.
  783. - *
  784. - * @note Use this sort for presorted / regular sorted data.
  785. - *
  786. - * @param base Pointer to the first element of the array to be sorted.
  787. - * @param num Number of elements in the array pointed by base.
  788. - * @param comparator Function that compares two elements.
  789. - * @param desc Sort descending.
  790. - */
  791. -template <typename T>
  792. -static inline void GSortT(T *base, uint num, int (CDECL *comparator)(const T*, const T*), bool desc = false)
  793. -{
  794. -       if (num < 2) return;
  795. -
  796. -       assert(base != NULL);
  797. -       assert(comparator != NULL);
  798. -
  799. -       T *a = base;
  800. -       T *b = base + 1;
  801. -       uint offset = 0;
  802. -
  803. -       while (num > 1) {
  804. -               const int diff = comparator(a, b);
  805. -               if ((!desc && diff <= 0) || (desc && diff >= 0)) {
  806. -                       if (offset != 0) {
  807. -                               /* Jump back to the last direction switch point */
  808. -                               a += offset;
  809. -                               b += offset;
  810. -                               offset = 0;
  811. -                               continue;
  812. -                       }
  813. -
  814. -                       a++;
  815. -                       b++;
  816. -                       num--;
  817. -               } else {
  818. -                       Swap(*a, *b);
  819. -
  820. -                       if (a == base) continue;
  821. -
  822. -                       a--;
  823. - * @param num Number of elements in the array pointed by base.
  824. - * @param comparator Function that compares two elements.
  825. - * @param desc Sort descending.
  826. - */
  827. -template <typename T>
  828. -static inline void QSortT(T *base, uint num, int (CDECL *comparator)(const T*, const T*), bool desc = false)
  829. -{
  830. -       if (num < 2) return;
  831. -
  832. -       qsort(base, num, sizeof(T), (int (CDECL *)(const void *, const void *))comparator);
  833. -
  834. -       if (desc) MemReverseT(base, num);
  835. -}
  836. -
  837. -/**
  838. - * Type safe Gnome Sort.
  839. - *
  840. - * This is a slightly modified Gnome search. The basic
  841. - * Gnome search tries to sort already sorted list parts.
  842. - * The modification skips these.
  843. - *
  844. - * @note Use this sort for presorted / regular sorted data.
  845. - *
  846. - * @param base Pointer to the first element of the array to be sorted.
  847. - * @param num Number of elements in the array pointed by base.
  848. - * @param comparator Function that compares two elements.
  849. - * @param desc Sort descending.
  850. - */
  851. -template <typename T>
  852. -static inline void GSortT(T *base, uint num, int (CDECL *comparator)(const T*, const T*), bool desc = false)
  853. -{
  854. -       if (num < 2) return;
  855. -
  856. -       assert(base != NULL);
  857. -       assert(comparator != NULL);
  858. -
  859. -       T *a = base;
  860. -       T *b = base + 1;
  861. -       uint offset = 0;
  862. -
  863. -       while (num > 1) {
  864. -               const int diff = comparator(a, b);
  865. -               if ((!desc && diff <= 0) || (desc && diff >= 0)) {
  866. -                       if (offset != 0) {
  867. -                               /* Jump back to the last direction switch point */
  868. -                               a += offset;
  869. -                               b += offset;
  870. -                               offset = 0;
  871. -                               continue;
  872. -                       }
  873. -
  874. -                       a++;
  875. -                       b++;
  876. -                       num--;
  877. -               } else {
  878. -                       Swap(*a, *b);
  879. -
  880. -                       if (a == base) continue;
  881. -
  882. -                       a--;
  883. -                       b--;
  884. -                       offset++;
  885. -               }
  886. -       }
  887. -}
  888. -
  889. -#endif /* SORT_FUNC_HPP */
  890. diff --git a/src/engine_gui.cpp b/src/engine_gui.cpp
  891. index 070ad6727..e82fd7e4e 100644
  892. --- a/src/engine_gui.cpp
  893. +++ b/src/engine_gui.cpp
  894. @@ -329,7 +329,7 @@ void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare)
  895.         /* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems)
  896.          * generally, do not sort if there are less than 2 items */
  897.         if (size < 2) return;
  898. -       QSortT(el->Begin(), size, compare);
  899. +       std::sort(el->Begin(), el->End(), compare);
  900.  }
  901.  
  902.  /**
  903. @@ -344,6 +344,6 @@ void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, ui
  904.         if (num_items < 2) return;
  905.         assert(begin < el->Length());
  906.         assert(begin + num_items <= el->Length());
  907. -       QSortT(el->Get(begin), num_items, compare);
  908. +       std::sort(el->Get(begin), el->Get(begin + num_items), compare);
  909.  }
  910.  
  911. diff --git a/src/engine_gui.h b/src/engine_gui.h
  912. index fc0b7ad7d..ac2ec718f 100644
  913. --- a/src/engine_gui.h
  914. +++ b/src/engine_gui.h
  915. @@ -19,7 +19,7 @@
  916.  
  917.  typedef GUIList<EngineID, CargoID> GUIEngineList;
  918.  
  919. -typedef int CDECL EngList_SortTypeFunction(const EngineID*, const EngineID*); ///< argument type for #EngList_Sort.
  920. +typedef bool CDECL EngList_SortTypeFunction(const EngineID&, const EngineID&); ///< argument type for #EngList_Sort.
  921.  void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare);
  922.  void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items);
  923.  
  924. diff --git a/src/fios.cpp b/src/fios.cpp
  925. index 8ed01152f..117667f90 100644
  926. --- a/src/fios.cpp
  927. +++ b/src/fios.cpp
  928. @@ -49,25 +49,19 @@ extern void GetOldSaveGameName(const char *file, char *title, const char *last);
  929.   * @param db A pointer to the second FiosItem to compare.
  930.   * @return -1, 0 or 1, depending on how the two items should be sorted.
  931.   */
  932. -int CDECL CompareFiosItems(const FiosItem *da, const FiosItem *db)
  933. +bool CDECL CompareFiosItems(const FiosItem &da, const FiosItem &db)
  934.  {
  935. -       int r = 0;
  936. -
  937. -       if ((_savegame_sort_order & SORT_BY_NAME) == 0 && da->mtime != db->mtime) {
  938. -               r = da->mtime < db->mtime ? -1 : 1;
  939. +       bool r;
  940. +       if ((_savegame_sort_order & SORT_BY_NAME) == 0 && da.mtime != db.mtime) {
  941. +               r = da.mtime < db.mtime;
  942.         } else {
  943. -               r = strcasecmp(da->title, db->title);
  944. +               r = strcasecmp(da.title, db.title) < 0;
  945.         }
  946.  
  947. -       if (_savegame_sort_order & SORT_DESCENDING) r = -r;
  948. +       if (_savegame_sort_order & SORT_DESCENDING) r = !r;
  949.         return r;
  950.  }
  951.  
  952. -FileList::~FileList()
  953. -{
  954. -       this->Clear();
  955. -}
  956. -
  957.  /**
  958.   * Construct a file list with the given kind of files, for the stated purpose.
  959.   * @param abstract_filetype Kind of files to collect.
  960. @@ -398,7 +392,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
  961.         {
  962.                 SortingBits order = _savegame_sort_order;
  963.                 _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
  964. -               QSortT(file_list.files.Begin(), file_list.files.Length(), CompareFiosItems);
  965. +               std::sort(file_list.Begin(), file_list.End(), CompareFiosItems);
  966.                 _savegame_sort_order = order;
  967.         }
  968.  
  969. @@ -413,7 +407,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
  970.                 scanner.Scan(NULL, subdir, true, true);
  971.         }
  972.  
  973. -       QSortT(file_list.Get(sort_start), file_list.Length() - sort_start, CompareFiosItems);
  974. +       std::sort(file_list.Get(sort_start), file_list.End(), CompareFiosItems);
  975.  
  976.         /* Show drives */
  977.         FiosGetDrives(file_list);
  978. diff --git a/src/fios.h b/src/fios.h
  979. index 5e17e8ee1..40f4852fe 100644
  980. --- a/src/fios.h
  981. +++ b/src/fios.h
  982. @@ -103,94 +103,10 @@ struct FiosItem {
  983.  };
  984.  
  985.  /** List of file information. */
  986. -class FileList {
  987. +class FileList : public SmallVector<FiosItem, 32>  {
  988.  public:
  989. -       ~FileList();
  990. -
  991. -       /**
  992. -        * Construct a new entry in the file list.
  993. -        * @return Pointer to the new items to be initialized.
  994. -        */
  995. -       inline FiosItem *Append()
  996. -       {
  997. -               return this->files.Append();
  998. -       }
  999. -
  1000. -       /**
  1001. -        * Get the number of files in the list.
  1002. -        * @return The number of files stored in the list.
  1003. -        */
  1004. -       inline uint Length() const
  1005. -       {
  1006. -               return this->files.Length();
  1007. -       }
  1008. -
  1009. -       /**
  1010. -        * Get a pointer to the first file information.
  1011. -        * @return Address of the first file information.
  1012. -        */
  1013. -       inline const FiosItem *Begin() const
  1014. -       {
  1015. -               return this->files.Begin();
  1016. -       }
  1017. -
  1018. -       /**
  1019. -        * Get a pointer behind the last file information.
  1020. -        * @return Address behind the last file information.
  1021. -        */
  1022. -       inline const FiosItem *End() const
  1023. -       {
  1024. -               return this->files.End();
  1025. -       }
  1026. -
  1027. -       /**
  1028. -        * Get a pointer to the indicated file information. File information must exist.
  1029. -        * @return Address of the indicated existing file information.
  1030. -        */
  1031. -       inline const FiosItem *Get(uint index) const
  1032. -       {
  1033. -               return this->files.Get(index);
  1034. -       }
  1035. -
  1036. -       /**
  1037. -        * Get a pointer to the indicated file information. File information must exist.
  1038. -        * @return Address of the indicated existing file information.
  1039. -        */
  1040. -       inline FiosItem *Get(uint index)
  1041. -       {
  1042. -               return this->files.Get(index);
  1043. -       }
  1044. -
  1045. -       inline const FiosItem &operator[](uint index) const
  1046. -       {
  1047. -               return this->files[index];
  1048. -       }
  1049. -
  1050. -       /**
  1051.  
  1052.         /* Show drives */
  1053.         FiosGetDrives(file_list);
  1054. diff --git a/src/fios.h b/src/fios.h
  1055. index 5e17e8ee1..40f4852fe 100644
  1056. --- a/src/fios.h
  1057. +++ b/src/fios.h
  1058. @@ -103,94 +103,10 @@ struct FiosItem {
  1059.  };
  1060.  
  1061.  /** List of file information. */
  1062. -class FileList {
  1063. +class FileList : public SmallVector<FiosItem, 32>  {
  1064.  public:
  1065. -       ~FileList();
  1066. -
  1067. -       /**
  1068. -        * Construct a new entry in the file list.
  1069. -        * @return Pointer to the new items to be initialized.
  1070. -        */
  1071. -       inline FiosItem *Append()
  1072. -       {
  1073. -               return this->files.Append();
  1074. -       }
  1075. -
  1076. -       /**
  1077. -        * Get the number of files in the list.
  1078. -        * @return The number of files stored in the list.
  1079. -        */
  1080. -       inline uint Length() const
  1081. -       {
  1082. -               return this->files.Length();
  1083. -       }
  1084. -
  1085. -       /**
  1086. -        * Get a pointer to the first file information.
  1087. -        * @return Address of the first file information.
  1088. -        */
  1089. -       inline const FiosItem *Begin() const
  1090. -       {
  1091. -               return this->files.Begin();
  1092. -       }
  1093. -
  1094. -       /**
  1095. -        * Get a pointer behind the last file information.
  1096. -        * @return Address behind the last file information.
  1097. -        */
  1098. -       inline const FiosItem *End() const
  1099. -       {
  1100. -               return this->files.End();
  1101. -       }
  1102. -
  1103. -       /**
  1104. -        * Get a pointer to the indicated file information. File information must exist.
  1105. -        * @return Address of the indicated existing file information.
  1106. -        */
  1107. -       inline const FiosItem *Get(uint index) const
  1108. -       {
  1109. -               return this->files.Get(index);
  1110. -       }
  1111. -
  1112. -       /**
  1113. -        * Get a pointer to the indicated file information. File information must exist.
  1114. -        * @return Address of the indicated existing file information.
  1115. -        */
  1116. -       inline FiosItem *Get(uint index)
  1117. -       {
  1118. -               return this->files.Get(index);
  1119. -       }
  1120. -
  1121. -       inline const FiosItem &operator[](uint index) const
  1122. -       {
  1123. -               return this->files[index];
  1124. -       }
  1125. -
  1126. -       /**
  1127.  
  1128.         /* Show drives */
  1129.         FiosGetDrives(file_list);
  1130. diff --git a/src/fios.h b/src/fios.h
  1131. index 5e17e8ee1..40f4852fe 100644
  1132. --- a/src/fios.h
  1133. +++ b/src/fios.h
  1134. @@ -103,94 +103,10 @@ struct FiosItem {
  1135.  };
  1136.  
  1137.  /** List of file information. */
  1138. -class FileList {
  1139. +class FileList : public SmallVector<FiosItem, 32>  {
  1140.  public:
  1141. -       ~FileList();
  1142. -
  1143. -       /**
  1144. -        * Construct a new entry in the file list.
  1145. -        * @return Pointer to the new items to be initialized.
  1146. -        */
  1147. -       inline FiosItem *Append()
  1148. -       {
  1149. -               return this->files.Append();
  1150. -       }
  1151. -
  1152. -       /**
  1153. -        * Get the number of files in the list.
  1154. -        * @return The number of files stored in the list.
  1155. -        */
  1156. -       inline uint Length() const
  1157. -       {
  1158. -               return this->files.Length();
  1159. -       }
  1160. -
  1161. -       /**
  1162. -        * Get a pointer to the first file information.
  1163. -        * @return Address of the first file information.
  1164. -        */
  1165. -       inline const FiosItem *Begin() const
  1166. -       {
  1167. -               return this->files.Begin();
  1168. -       }
  1169. -
  1170. -       /**
  1171. -        * Get a pointer behind the last file information.
  1172. -        * @return Address behind the last file information.
  1173. -        */
  1174. -       inline const FiosItem *End() const
  1175. -       {
  1176. -               return this->files.End();
  1177. -       }
  1178. -
  1179. -       /**
  1180. -        * Get a pointer to the indicated file information. File information must exist.
  1181. -        * @return Address of the indicated existing file information.
  1182. -        */
  1183. -       inline const FiosItem *Get(uint index) const
  1184. -       {
  1185. -               return this->files.Get(index);
  1186. -       }
  1187. -
  1188. -       /**
  1189. -        * Get a pointer to the indicated file information. File information must exist.
  1190. -        * @return Address of the indicated existing file information.
  1191. -        */
  1192. -       inline FiosItem *Get(uint index)
  1193. -       {
  1194. -               return this->files.Get(index);
  1195. -       }
  1196. -
  1197. -       inline const FiosItem &operator[](uint index) const
  1198. -       {
  1199. -               return this->files[index];
  1200. -       }
  1201. -
  1202. -       /**
  1203. -        * Get a reference to the indicated file information. File information must exist.
  1204. -        * @return The requested file information.
  1205. -        */
  1206. -       inline FiosItem &operator[](uint index)
  1207. -       {
  1208. -               return this->files[index];
  1209. -       }
  1210. -
  1211. -       /** Remove all items from the list. */
  1212. -       inline void Clear()
  1213. -       {
  1214. -               this->files.Clear();
  1215. -       }
  1216. -
  1217. -       /** Compact the list down to the smallest block size boundary. */
  1218. -       inline void Compact()
  1219. -       {
  1220. -               this->files.Compact();
  1221. -       }
  1222. -
  1223.         void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop);
  1224.         const FiosItem *FindItem(const char *file);
  1225. -
  1226. -       SmallVector<FiosItem, 32> files; ///< The list of files.
  1227.  };
  1228.  
  1229.  enum SortingBits {
  1230. @@ -219,6 +135,6 @@ void FiosMakeSavegameName(char *buf, const char *name, const char *last);
  1231.  
  1232.  FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last);
  1233.  
  1234. -int CDECL CompareFiosItems(const FiosItem *a, const FiosItem *b);
  1235. +bool CDECL CompareFiosItems(const FiosItem &a, const FiosItem &b);
  1236.  
  1237.  #endif /* FIOS_H */
  1238. diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp
  1239. index e6cd9625c..64eac2a50 100644
  1240. --- a/src/fios_gui.cpp
  1241. +++ b/src/fios_gui.cpp
  1242. @@ -221,8 +221,7 @@ static void SortSaveGameList(FileList &file_list)
  1243.                 }
  1244.         }
  1245.  
  1246. -       uint s_amount = file_list.Length() - sort_start - sort_end;
  1247. -       QSortT(file_list.Get(sort_start), s_amount, CompareFiosItems);
  1248. +       std::sort(file_list.Get(sort_start), file_list.Get(sort_end), CompareFiosItems);
  1249.  }
  1250.  
  1251.  struct SaveLoadWindow : public Window {
  1252. diff --git a/src/gfx.cpp b/src/gfx.cpp
  1253. index 719505157..efe6e03eb 100644
  1254. --- a/src/gfx.cpp
  1255. +++ b/src/gfx.cpp
  1256. @@ -1699,14 +1699,12 @@ bool ToggleFullScreen(bool fs)
  1257.         return result;
  1258.  }
  1259.  
  1260. -static int CDECL compare_res(const Dimension *pa, const Dimension *pb)
  1261. +static bool CDECL compare_res(const Dimension &pa, const Dimension &pb)
  1262.  {
  1263. -       int x = pa->width - pb->width;
  1264. -       if (x != 0) return x;
  1265. -       return pa->height - pb->height;
  1266. +       return pa.width < pb.width || pa.height < pb.height;
  1267.  }
  1268.  
  1269.  void SortResolutions(int count)
  1270.  {
  1271. -       QSortT(_resolutions, count, &compare_res);
  1272. +       std::sort(_resolutions, _resolutions + count, &compare_res);
  1273.  }
  1274. diff --git a/src/graph_gui.cpp b/src/graph_gui.cpp
  1275. index c12c6ace4..16fc1107a 100644
  1276. --- a/src/graph_gui.cpp
  1277. +++ b/src/graph_gui.cpp
  1278. @@ -1151,9 +1151,9 @@ private:
  1279.         }
  1280.  
  1281.         /** Sort the company league by performance history */
  1282. -       static int CDECL PerformanceSorter(const Company * const *c1, const Company * const *c2)
  1283. +       static bool CDECL PerformanceSorter(const Company * const &c1, const Company * const &c2)
  1284.         {
  1285. -               return (*c2)->old_economy[0].performance_history - (*c1)->old_economy[0].performance_history;
  1286. +               return c2->old_economy[0].performance_history < c1->old_economy[0].performance_history;
  1287.         }
  1288.  
  1289.  public:
  1290. diff --git a/src/group_gui.cpp b/src/group_gui.cpp
  1291. index d3e1eafbb..713b11cdb 100644
  1292. --- a/src/group_gui.cpp
  1293. +++ b/src/group_gui.cpp
  1294. @@ -134,26 +134,26 @@ private:
  1295.         }
  1296.  
  1297.         /** Sort the groups by their name */
  1298. -       static int CDECL GroupNameSorter(const Group * const *a, const Group * const *b)
  1299. +       static bool CDECL GroupNameSorter(const Group * const &a, const Group * const &b)
  1300.         {
  1301.                 static const Group *last_group[2] = { NULL, NULL };
  1302.                 static char         last_name[2][64] = { "", "" };
  1303.  
  1304. -               if (*a != last_group[0]) {
  1305. -                       last_group[0] = *a;
  1306. -                       SetDParam(0, (*a)->index);
  1307. +               if (a != last_group[0]) {
  1308. +                       last_group[0] = a;
  1309. +                       SetDParam(0, a->index);
  1310.                         GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
  1311.                 }
  1312.  
  1313. -               if (*b != last_group[1]) {
  1314. -                       last_group[1] = *b;
  1315. -                       SetDParam(0, (*b)->index);
  1316. +               if (b != last_group[1]) {
  1317. +                       last_group[1] = b;
  1318. +                       SetDParam(0, b->index);
  1319.                         GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
  1320.                 }
  1321.  
  1322.                 int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
  1323. -               if (r == 0) return (*a)->index - (*b)->index;
  1324. -               return r;
  1325. +               if (r == 0) return a->index < b->index;
  1326. +               return r < 0;
  1327.         }
  1328.  
  1329.         /**
  1330. diff --git a/src/highscore.cpp b/src/highscore.cpp
  1331. index 86e4f5ae8..57bb2e9f3 100644
  1332. --- a/src/highscore.cpp
  1333. +++ b/src/highscore.cpp
  1334. @@ -10,6 +10,9 @@
  1335.  /** @file highscore.cpp Definition of functions used for highscore handling */
  1336.  
  1337.  #include "stdafx.h"
  1338. +
  1339. +#include <algorithm>
  1340. +
  1341.  #include "highscore.h"
  1342.  #include "company_base.h"
  1343.  #include "company_func.h"
  1344. @@ -17,7 +20,6 @@
  1345.  #include "string_func.h"
  1346.  #include "strings_func.h"
  1347.  #include "table/strings.h"
  1348. -#include "core/sort_func.hpp"
  1349.  #include "debug.h"
  1350.  
  1351.  #include "safeguards.h"
  1352. @@ -79,9 +81,9 @@ int8 SaveHighScoreValue(const Company *c)
  1353.  }
  1354.  
  1355.  /** Sort all companies given their performance */
  1356. -static int CDECL HighScoreSorter(const Company * const *a, const Company * const *b)
  1357. +static bool CDECL HighScoreSorter(const Company * const &a, const Company * const &b)
  1358.  {
  1359. -       return (*b)->old_economy[0].performance_history - (*a)->old_economy[0].performance_history;
  1360. +       return b->old_economy[0].performance_history < a->old_economy[0].performance_history;
  1361.  }
  1362.  
  1363.  /**
  1364. @@ -98,7 +100,7 @@ int8 SaveHighScoreValueNetwork()
  1365.         /* Sort all active companies with the highest score first */
  1366.         FOR_ALL_COMPANIES(c) cl[count++] = c;
  1367.  
  1368. -       QSortT(cl, count, &HighScoreSorter);
  1369. +       std::sort(cl, cl + count, &HighScoreSorter);
  1370.  
  1371.         {
  1372.                 uint i;
  1373. diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp
  1374. index b9077b9d7..9e152649b 100644
  1375. --- a/src/industry_gui.cpp
  1376. +++ b/src/industry_gui.cpp
  1377. @@ -160,20 +160,20 @@ static inline void GetAllCargoSuffixes(uint cb_offset, CargoSuffixType cst, cons
  1378.  IndustryType _sorted_industry_types[NUM_INDUSTRYTYPES]; ///< Industry types sorted by name.
  1379.  
  1380.  /** Sort industry types by their name. */
  1381. -static int CDECL IndustryTypeNameSorter(const IndustryType *a, const IndustryType *b)
  1382. +static bool CDECL IndustryTypeNameSorter(const IndustryType &a, const IndustryType &b)
  1383.  {
  1384.         static char industry_name[2][64];
  1385.  
  1386. -       const IndustrySpec *indsp1 = GetIndustrySpec(*a);
  1387. +       const IndustrySpec *indsp1 = GetIndustrySpec(a);
  1388.         GetString(industry_name[0], indsp1->name, lastof(industry_name[0]));
  1389.  
  1390. -       const IndustrySpec *indsp2 = GetIndustrySpec(*b);
  1391. +       const IndustrySpec *indsp2 = GetIndustrySpec(b);
  1392.         GetString(industry_name[1], indsp2->name, lastof(industry_name[1]));
  1393.  
  1394.         int r = strnatcmp(industry_name[0], industry_name[1]); // Sort by name (natural sorting).
  1395.  
  1396.         /* If the names are equal, sort by industry type. */
  1397. -       return (r != 0) ? r : (*a - *b);
  1398. +       return (r != 0) ? r < 0 : (a < b);
  1399.  }
  1400.  
  1401.  /**
  1402. @@ -187,7 +187,8 @@ void SortIndustryTypes()
  1403.         }
  1404.  
  1405.         /* Sort industry types by name. */
  1406. -       QSortT(_sorted_industry_types, NUM_INDUSTRYTYPES, &IndustryTypeNameSorter);
  1407. +
  1408. +       std::sort(std::begin(_sorted_industry_types), std::end(_sorted_industry_types), IndustryTypeNameSorter);
  1409.  }
  1410.  
  1411.  /**
  1412. @@ -1201,52 +1202,49 @@ protected:
  1413.         }
  1414.  
  1415.         /** Sort industries by name */
  1416. -       static int CDECL IndustryNameSorter(const Industry * const *a, const Industry * const *b)
  1417. +       static bool CDECL IndustryNameSorter(const Industry * const &a, const Industry * const &b)
  1418.         {
  1419.                 static char buf_cache[96];
  1420.                 static char buf[96];
  1421.  
  1422. -               SetDParam(0, (*a)->index);
  1423. +               SetDParam(0, a->index);
  1424.                 GetString(buf, STR_INDUSTRY_NAME, lastof(buf));
  1425.  
  1426. -               if (*b != last_industry) {
  1427. -                       last_industry = *b;
  1428. -                       SetDParam(0, (*b)->index);
  1429. +               if (b != last_industry) {
  1430. +                       last_industry = b;
  1431. +                       SetDParam(0, b->index);
  1432.                         GetString(buf_cache, STR_INDUSTRY_NAME, lastof(buf_cache));
  1433.                 }
  1434.  
  1435. -               return strnatcmp(buf, buf_cache); // Sort by name (natural sorting).
  1436. +               return strnatcmp(buf, buf_cache) < 0; // Sort by name (natural sorting).
  1437.         }
  1438.  
  1439.         /** Sort industries by type and name */
  1440. -       static int CDECL IndustryTypeSorter(const Industry * const *a, const Industry * const *b)
  1441. +       static bool CDECL IndustryTypeSorter(const Industry * const &a, const Industry * const &b)
  1442.         {
  1443.                 int it_a = 0;
  1444. -               while (it_a != NUM_INDUSTRYTYPES && (*a)->type != _sorted_industry_types[it_a]) it_a++;
  1445. +               while (it_a != NUM_INDUSTRYTYPES && a->type != _sorted_industry_types[it_a]) it_a++;
  1446.                 int it_b = 0;
  1447. -               while (it_b != NUM_INDUSTRYTYPES && (*b)->type != _sorted_industry_types[it_b]) it_b++;
  1448. -               int r = it_a - it_b;
  1449. -               return (r == 0) ? IndustryNameSorter(a, b) : r;
  1450. +               while (it_b != NUM_INDUSTRYTYPES && b->type != _sorted_industry_types[it_b]) it_b++;
  1451. +               return (it_a == it_b) ? IndustryNameSorter(a, b) : it_a < it_b;
  1452.         }
  1453.  
  1454.         /** Sort industries by production and name */
  1455. -       static int CDECL IndustryProductionSorter(const Industry * const *a, const Industry * const *b)
  1456. +       static bool CDECL IndustryProductionSorter(const Industry * const &a, const Industry * const &b)
  1457.         {
  1458.                 uint prod_a = 0, prod_b = 0;
  1459. -               for (uint i = 0; i < lengthof((*a)->produced_cargo); i++) {
  1460. -                       if ((*a)->produced_cargo[i] != CT_INVALID) prod_a += (*a)->last_month_production[i];
  1461. -                       if ((*b)->produced_cargo[i] != CT_INVALID) prod_b += (*b)->last_month_production[i];
  1462. +               for (uint i = 0; i < lengthof(a->produced_cargo); i++) {
  1463. +                       if (a->produced_cargo[i] != CT_INVALID) prod_a += a->last_month_production[i];
  1464. +                       if (b->produced_cargo[i] != CT_INVALID) prod_b += b->last_month_production[i];
  1465.                 }
  1466. -               int r = prod_a - prod_b;
  1467. -
  1468. -               return (r == 0) ? IndustryTypeSorter(a, b) : r;
  1469. +               return (prod_a == prod_b) ? IndustryTypeSorter(a, b) : prod_a < prod_b;
  1470.         }
  1471.  
  1472.         /** Sort industries by transported cargo and name */
  1473. -       static int CDECL IndustryTransportedCargoSorter(const Industry * const *a, const Industry * const *b)
  1474. +       static bool CDECL IndustryTransportedCargoSorter(const Industry * const &a, const Industry * const &b)
  1475.         {
  1476. -               int r = GetCargoTransportedSortValue(*a) - GetCargoTransportedSortValue(*b);
  1477. -               return (r == 0) ? IndustryNameSorter(a, b) : r;
  1478. +               int r = GetCargoTransportedSortValue(a) - GetCargoTransportedSortValue(b);
  1479. +               return (r == 0) ? IndustryNameSorter(a, b) : r < 0;
  1480.         }
  1481.  
  1482.         /**
  1483. diff --git a/src/network/network_content_gui.cpp b/src/network/network_content_gui.cpp
  1484. index 1227d43db..a152d447c 100644
  1485. --- a/src/network/network_content_gui.cpp
  1486. +++ b/src/network/network_content_gui.cpp
  1487. @@ -407,28 +407,29 @@ class NetworkContentListWindow : public Window, ContentCallback {
  1488.         }
  1489.  
  1490.         /** Sort content by name. */
  1491. -       static int CDECL NameSorter(const ContentInfo * const *a, const ContentInfo * const *b)
  1492. +       static bool CDECL NameSorter(const ContentInfo * const &a, const ContentInfo * const &b)
  1493.         {
  1494. -               return strnatcmp((*a)->name, (*b)->name, true); // Sort by name (natural sorting).
  1495. +               return strnatcmp(a->name, b->name, true) < 0; // Sort by name (natural sorting).
  1496.         }
  1497.  
  1498.         /** Sort content by type. */
  1499. -       static int CDECL TypeSorter(const ContentInfo * const *a, const ContentInfo * const *b)
  1500. +       static bool CDECL TypeSorter(const ContentInfo * const &a, const ContentInfo * const &b)
  1501.         {
  1502.                 int r = 0;
  1503. -               if ((*a)->type != (*b)->type) {
  1504. -                       r = strnatcmp(content_type_strs[(*a)->type], content_type_strs[(*b)->type]);
  1505. +               if (a->type != b->type) {
  1506. +                       r = strnatcmp(content_type_strs[a->type], content_type_strs[b->type]);
  1507.                 }
  1508. -               if (r == 0) r = NameSorter(a, b);
  1509. -               return r;
  1510. +               if (r == 0) return NameSorter(a, b);
  1511. +               return r < 0;
  1512.         }
  1513.  
  1514.         /** Sort content by state. */
  1515. -       static int CDECL StateSorter(const ContentInfo * const *a, const ContentInfo * const *b)
  1516. +       static bool CDECL StateSorter(const ContentInfo * const &a, const ContentInfo * const &b)
  1517.         {
  1518. -               int r = (*a)->state - (*b)->state;
  1519. -               if (r == 0) r = TypeSorter(a, b);
  1520. -               return r;
  1521. +               if (a->state != b->state) {
  1522. +                       return a->state < b->state;
  1523. +               }
  1524. +               return TypeSorter(a, b);
  1525.         }
  1526.  
  1527.         /** Sort the content list */
  1528. diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp
  1529. index 520c4f60a..ac8d7d950 100644
  1530. --- a/src/network/network_gui.cpp
  1531. +++ b/src/network/network_gui.cpp
  1532. @@ -74,7 +74,7 @@ void SortNetworkLanguages()
  1533.         }
  1534.  
  1535.         /* Sort the strings (we don't move 'any' and the 'invalid' one) */
  1536. -       QSortT(_language_dropdown + 1, NETLANG_COUNT - 1, &StringIDSorter);
  1537. +       std::sort(_language_dropdown + 1, _language_dropdown + NETLANG_COUNT - 1, &StringIDSorter);
  1538.  }
  1539.  
  1540.  /**
  1541. @@ -276,10 +276,10 @@ protected:
  1542.         }
  1543.  
  1544.         /** Sort servers by name. */
  1545. -       static int CDECL NGameNameSorter(NetworkGameList * const *a, NetworkGameList * const *b)
  1546. +       static bool CDECL NGameNameSorter(NetworkGameList * const &a, NetworkGameList * const &b)
  1547.         {
  1548. -               int r = strnatcmp((*a)->info.server_name, (*b)->info.server_name, true); // Sort by name (natural sorting).
  1549. -               return r == 0 ? (*a)->address.CompareTo((*b)->address) : r;
  1550. +               int r = strnatcmp(a->info.server_name, b->info.server_name, true); // Sort by name (natural sorting).
  1551. +               return r == 0 ? a->address.CompareTo(b->address) < 0 : r < 0;
  1552.         }
  1553.  
  1554.         /**
  1555. @@ -287,60 +287,60 @@ protected:
  1556.          * server. If the two servers have the same amount, the one with the
  1557.          * higher maximum is preferred.
  1558.          */
  1559. -       static int CDECL NGameClientSorter(NetworkGameList * const *a, NetworkGameList * const *b)
  1560. +       static bool CDECL NGameClientSorter(NetworkGameList * const &a, NetworkGameList * const &b)
  1561.         {
  1562.                 /* Reverse as per default we are interested in most-clients first */
  1563. -               int r = (*a)->info.clients_on - (*b)->info.clients_on;
  1564. +               int r = a->info.clients_on - b->info.clients_on;
  1565.  
  1566. -               if (r == 0) r = (*a)->info.clients_max - (*b)->info.clients_max;
  1567. -               if (r == 0) r = NGameNameSorter(a, b);
  1568. +               if (r == 0) r = a->info.clients_max - b->info.clients_max;
  1569. +               if (r == 0) return NGameNameSorter(a, b);
  1570.  
  1571. -               return r;
  1572. +               return r < 0;
  1573.         }
  1574.  
  1575.         /** Sort servers by map size */
  1576. -       static int CDECL NGameMapSizeSorter(NetworkGameList * const *a, NetworkGameList * const *b)
  1577. +       static bool CDECL NGameMapSizeSorter(NetworkGameList * const &a, NetworkGameList * const &b)
  1578.         {
  1579.                 /* Sort by the area of the map. */
  1580. -               int r = ((*a)->info.map_height) * ((*a)->info.map_width) - ((*b)->info.map_height) * ((*b)->info.map_width);
  1581. +               int r = (a->info.map_height) * (a->info.map_width) - (b->info.map_height) * (b->info.map_width);
  1582.  
  1583. -               if (r == 0) r = (*a)->info.map_width - (*b)->info.map_width;
  1584. -               return (r != 0) ? r : NGameClientSorter(a, b);
  1585. +               if (r == 0) r = a->info.map_width - b->info.map_width;
  1586. +               return (r != 0) ? r < 0 : NGameClientSorter(a, b);
  1587.         }
  1588.  
  1589.         /** Sort servers by current date */
  1590. -       static int CDECL NGameDateSorter(NetworkGameList * const *a, NetworkGameList * const *b)
  1591. +       static bool CDECL NGameDateSorter(NetworkGameList * const &a, NetworkGameList * const &b)
  1592.         {
  1593. -               int r = (*a)->info.game_date - (*b)->info.game_date;
  1594. -               return (r != 0) ? r : NGameClientSorter(a, b);
  1595. +               int r = a->info.game_date - b->info.game_date;
  1596. +               return (r != 0) ? r < 0 : NGameClientSorter(a, b);
  1597.         }
  1598.  
  1599.         /** Sort servers by the number of days the game is running */
  1600. -       static int CDECL NGameYearsSorter(NetworkGameList * const *a, NetworkGameList * const *b)
  1601. +       static bool CDECL NGameYearsSorter(NetworkGameList * const &a, NetworkGameList * const &b)
  1602.         {
  1603. -               int r = (*a)->info.game_date - (*a)->info.start_date - (*b)->info.game_date + (*b)->info.start_date;
  1604. -               return (r != 0) ? r : NGameDateSorter(a, b);
  1605. +               int r = a->info.game_date - a->info.start_date - b->info.game_date + b->info.start_date;
  1606. +               return (r != 0) ? r < 0 : NGameDateSorter(a, b);
  1607.         }
  1608.  
  1609.         /**
  1610.          * Sort servers by joinability. If both servers are the
  1611.          * same, prefer the non-passworded server first.
  1612.          */
  1613. -       static int CDECL NGameAllowedSorter(NetworkGameList * const *a, NetworkGameList * const *b)
  1614. +       static bool CDECL NGameAllowedSorter(NetworkGameList * const &a, NetworkGameList * const &b)
  1615.         {
  1616.                 /* The servers we do not know anything about (the ones that did not reply) should be at the bottom) */
  1617. -               int r = StrEmpty((*a)->info.server_revision) - StrEmpty((*b)->info.server_revision);
  1618. +               int r = StrEmpty(a->info.server_revision) - StrEmpty(b->info.server_revision);
  1619.  
  1620.                 /* Reverse default as we are interested in version-compatible clients first */
  1621. -               if (r == 0) r = (*b)->info.version_compatible - (*a)->info.version_compatible;
  1622. +               if (r == 0) r = b->info.version_compatible - a->info.version_compatible;
  1623.                 /* The version-compatible ones are then sorted with NewGRF compatible first, incompatible last */
  1624. -               if (r == 0) r = (*b)->info.compatible - (*a)->info.compatible;
  1625. +               if (r == 0) r = b->info.compatible - a->info.compatible;
  1626.                 /* Passworded servers should be below unpassworded servers */
  1627. -               if (r == 0) r = (*a)->info.use_password - (*b)->info.use_password;
  1628. +               if (r == 0) r = a->info.use_password - b->info.use_password;
  1629.                 /* Finally sort on the number of clients of the server */
  1630. -               if (r == 0) r = -NGameClientSorter(a, b);
  1631. +               if (r == 0) return !NGameClientSorter(a, b);
  1632.  
  1633. -               return r;
  1634. +               return r < 0;
  1635.         }
  1636.  
  1637.         /** Sort the server list */
  1638. diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp
  1639. index 593851e28..beffb2ed8 100644
  1640. --- a/src/newgrf_config.cpp
  1641. +++ b/src/newgrf_config.cpp
  1642. @@ -10,6 +10,9 @@
  1643.  /** @file newgrf_config.cpp Finding NewGRFs and configuring them. */
  1644.  
  1645.  #include "stdafx.h"
  1646. +
  1647. +#include <algorithm>
  1648. +
  1649.  #include "debug.h"
  1650.  #include "3rdparty/md5/md5.h"
  1651.  #include "newgrf.h"
  1652. @@ -711,12 +714,9 @@ bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length, const
  1653.   * @param p2 the second GRFConfig *
  1654.   * @return the same strcmp would return for the name of the NewGRF.
  1655.   */
  1656. -static int CDECL GRFSorter(GRFConfig * const *p1, GRFConfig * const *p2)
  1657. +static bool CDECL GRFSorter(const GRFConfig * const p1, const GRFConfig * const p2)
  1658.  {
  1659. -               if (r == 0) r = (*a)->info.map_width - (*b)->info.map_width;
  1660. -               return (r != 0) ? r : NGameClientSorter(a, b);
  1661. +               if (r == 0) r = a->info.map_width - b->info.map_width;
  1662. +               return (r != 0) ? r < 0 : NGameClientSorter(a, b);
  1663.         }
  1664.  
  1665.         /** Sort servers by current date */
  1666. -       static int CDECL NGameDateSorter(NetworkGameList * const *a, NetworkGameList * const *b)
  1667. +       static bool CDECL NGameDateSorter(NetworkGameList * const &a, NetworkGameList * const &b)
  1668.         {
  1669. -               int r = (*a)->info.game_date - (*b)->info.game_date;
  1670. -               return (r != 0) ? r : NGameClientSorter(a, b);
  1671. +               int r = a->info.game_date - b->info.game_date;
  1672. +               return (r != 0) ? r < 0 : NGameClientSorter(a, b);
  1673.         }
  1674.  
  1675.         /** Sort servers by the number of days the game is running */
  1676. -       static int CDECL NGameYearsSorter(NetworkGameList * const *a, NetworkGameList * const *b)
  1677. +       static bool CDECL NGameYearsSorter(NetworkGameList * const &a, NetworkGameList * const &b)
  1678.         {
  1679. -               int r = (*a)->info.game_date - (*a)->info.start_date - (*b)->info.game_date + (*b)->info.start_date;
  1680. -               return (r != 0) ? r : NGameDateSorter(a, b);
  1681. +               int r = a->info.game_date - a->info.start_date - b->info.game_date + b->info.start_date;
  1682. +               return (r != 0) ? r < 0 : NGameDateSorter(a, b);
  1683.         }
  1684.  
  1685.         /**
  1686.          * Sort servers by joinability. If both servers are the
  1687.          * same, prefer the non-passworded server first.
  1688.          */
  1689. -       static int CDECL NGameAllowedSorter(NetworkGameList * const *a, NetworkGameList * const *b)
  1690. +       static bool CDECL NGameAllowedSorter(NetworkGameList * const &a, NetworkGameList * const &b)
  1691.         {
  1692.                 /* The servers we do not know anything about (the ones that did not reply) should be at the bottom) */
  1693. -               int r = StrEmpty((*a)->info.server_revision) - StrEmpty((*b)->info.server_revision);
  1694. +               int r = StrEmpty(a->info.server_revision) - StrEmpty(b->info.server_revision);
  1695.  
  1696.                 /* Reverse default as we are interested in version-compatible clients first */
  1697. -               if (r == 0) r = (*b)->info.version_compatible - (*a)->info.version_compatible;
  1698. +               if (r == 0) r = b->info.version_compatible - a->info.version_compatible;
  1699.                 /* The version-compatible ones are then sorted with NewGRF compatible first, incompatible last */
  1700. -               if (r == 0) r = (*b)->info.compatible - (*a)->info.compatible;
  1701. +               if (r == 0) r = b->info.compatible - a->info.compatible;
  1702.                 /* Passworded servers should be below unpassworded servers */
  1703. -               if (r == 0) r = (*a)->info.use_password - (*b)->info.use_password;
  1704. +               if (r == 0) r = a->info.use_password - b->info.use_password;
  1705.                 /* Finally sort on the number of clients of the server */
  1706. -               if (r == 0) r = -NGameClientSorter(a, b);
  1707. +               if (r == 0) return !NGameClientSorter(a, b);
  1708.  
  1709. -               return r;
  1710. +               return r < 0;
  1711.         }
  1712.  
  1713.         /** Sort the server list */
  1714. diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp
  1715. index 593851e28..beffb2ed8 100644
  1716. --- a/src/newgrf_config.cpp
  1717. +++ b/src/newgrf_config.cpp
  1718. @@ -10,6 +10,9 @@
  1719.  /** @file newgrf_config.cpp Finding NewGRFs and configuring them. */
  1720.  
  1721.  #include "stdafx.h"
  1722. +
  1723. +#include <algorithm>
  1724. +
  1725.  #include "debug.h"
  1726.  #include "3rdparty/md5/md5.h"
  1727.  #include "newgrf.h"
  1728. @@ -711,12 +714,9 @@ bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length, const
  1729.   * @param p2 the second GRFConfig *
  1730.   * @return the same strcmp would return for the name of the NewGRF.
  1731.   */
  1732. -static int CDECL GRFSorter(GRFConfig * const *p1, GRFConfig * const *p2)
  1733. +static bool CDECL GRFSorter(const GRFConfig * const p1, const GRFConfig * const p2)
  1734.  {
  1735. -       const GRFConfig *c1 = *p1;
  1736. -       const GRFConfig *c2 = *p2;
  1737. -
  1738. -       return strnatcmp(c1->GetName(), c2->GetName());
  1739. +       return strnatcmp(p1->GetName(), p2->GetName()) < 0;
  1740.  }
  1741.  
  1742.  /**
  1743. @@ -747,7 +747,7 @@ void DoScanNewGRFFiles(void *callback)
  1744.                 /* Number of files is not necessarily right */
  1745.                 num = i;
  1746.  
  1747. -               QSortT(to_sort, num, &GRFSorter);
  1748. +               std::sort(to_sort, to_sort + num, GRFSorter);
  1749.  
  1750.                 for (i = 1; i < num; i++) {
  1751.                         to_sort[i - 1]->next = to_sort[i];
  1752. diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp
  1753. index 8dd8d545a..e3e821f75 100644
  1754. --- a/src/newgrf_engine.cpp
  1755. +++ b/src/newgrf_engine.cpp
  1756. @@ -10,6 +10,9 @@
  1757.  /** @file newgrf_engine.cpp NewGRF handling of engines. */
  1758.  
  1759.  #include "stdafx.h"
  1760. +
  1761. +#include <algorithm>
  1762. +
  1763.  #include "debug.h"
  1764.  #include "train.h"
  1765.  #include "roadveh.h"
  1766. @@ -1232,19 +1235,19 @@ void AlterVehicleListOrder(EngineID engine, uint target)
  1767.   * @param b right side
  1768.   * @return comparison result
  1769.   */
  1770. -static int CDECL EnginePreSort(const EngineID *a, const EngineID *b)
  1771. +static bool CDECL EnginePreSort(const EngineID a, const EngineID b)
  1772.  {
  1773. -       const EngineIDMapping *id_a = _engine_mngr.Get(*a);
  1774. -       const EngineIDMapping *id_b = _engine_mngr.Get(*b);
  1775. +       const EngineIDMapping *id_a = _engine_mngr.Get(a);
  1776. +       const EngineIDMapping *id_b = _engine_mngr.Get(b);
  1777.  
  1778.         /* 1. Sort by engine type */
  1779. -       if (id_a->type != id_b->type) return (int)id_a->type - (int)id_b->type;
  1780. +       if (id_a->type != id_b->type) return id_a->type < id_b->type;
  1781.  
  1782.         /* 2. Sort by scope-GRFID */
  1783. -       if (id_a->grfid != id_b->grfid) return id_a->grfid < id_b->grfid ? -1 : 1;
  1784. +       if (id_a->grfid != id_b->grfid) return id_a->grfid < id_b->grfid;
  1785.  
  1786.         /* 3. Sort by local ID */
  1787. -       return (int)id_a->internal_id - (int)id_b->internal_id;
  1788. +       return id_a->internal_id < id_b->internal_id;
  1789.  }
  1790.  
  1791.  /**
  1792. @@ -1258,7 +1261,7 @@ void CommitVehicleListOrderChanges()
  1793.         FOR_ALL_ENGINES(e) {
  1794.                 *ordering.Append() = e->index;
  1795.         }
  1796. -       QSortT(ordering.Begin(), ordering.Length(), EnginePreSort);
  1797. +       std::sort(ordering.Begin(), ordering.End(), EnginePreSort);
  1798.  
  1799.         /* Apply Insertion-Sort operations */
  1800.         const ListOrderChange *end = _list_order_changes.End();
  1801. diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp
  1802. index c3007ac42..ff93986ed 100644
  1803. --- a/src/newgrf_gui.cpp
  1804. +++ b/src/newgrf_gui.cpp
  1805. @@ -1434,15 +1434,12 @@ struct NewGRFWindow : public Window, NewGRFScanCallback {
  1806.  
  1807.  private:
  1808.         /** Sort grfs by name. */
  1809. -       static int CDECL NameSorter(const GRFConfig * const *a, const GRFConfig * const *b)
  1810. +       static bool CDECL NameSorter(const GRFConfig * const &a, const GRFConfig * const &b)
  1811.         {
  1812. -               int i = strnatcmp((*a)->GetName(), (*b)->GetName(), true); // Sort by name (natural sorting).
  1813. -               if (i != 0) return i;
  1814. +               int i = strnatcmp(a->GetName(), b->GetName(), true); // Sort by name (natural sorting).
  1815. +               if (i == 0) i = a->version - b->version;
  1816.  
  1817. -               i = (*a)->version - (*b)->version;
  1818. -               if (i != 0) return i;
  1819. -
  1820. -               return memcmp((*a)->ident.md5sum, (*b)->ident.md5sum, lengthof((*b)->ident.md5sum));
  1821. +               return i == 0 ? memcmp(a->ident.md5sum, b->ident.md5sum, lengthof(b->ident.md5sum)) < 0 : i < 0;
  1822.         }
  1823.  
  1824.         /** Filter grfs by tags/name */
  1825. diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp
  1826. index 5582666b1..f01adda78 100644
  1827. --- a/src/rail_cmd.cpp
  1828. +++ b/src/rail_cmd.cpp
  1829. @@ -127,9 +127,9 @@ void ResolveRailTypeGUISprites(RailtypeInfo *rti)
  1830.   * @param second The railtype to compare.
  1831.   * @return True iff the first should be sorted before the second.
  1832.   */
  1833. -static int CDECL CompareRailTypes(const RailType *first, const RailType *second)
  1834. +static bool CDECL CompareRailTypes(const RailType &first, const RailType &second)
  1835.  {
  1836. -       return GetRailTypeInfo(*first)->sorting_order - GetRailTypeInfo(*second)->sorting_order;
  1837. +       return GetRailTypeInfo(first)->sorting_order < GetRailTypeInfo(second)->sorting_order;
  1838.  }
  1839.  
  1840.  /**
  1841. @@ -148,7 +148,7 @@ void InitRailTypes()
  1842.                         _sorted_railtypes[_sorted_railtypes_size++] = rt;
  1843.                 }
  1844.         }
  1845. -       QSortT(_sorted_railtypes, _sorted_railtypes_size, CompareRailTypes);
  1846. +       std::sort(_sorted_railtypes, _sorted_railtypes + _sorted_railtypes_size, CompareRailTypes);
  1847.  }
  1848.  
  1849.  /**
  1850. diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp
  1851. index 6cde709b0..4465a5ae7 100644
  1852. --- a/src/settings_gui.cpp
  1853. +++ b/src/settings_gui.cpp
  1854. @@ -203,7 +203,7 @@ struct GameOptionsWindow : Window {
  1855.                                         if (i == CURRENCY_CUSTOM) continue;
  1856.                                         *list->Append() = new DropDownListStringItem(*items, i, HasBit(disabled, i));
  1857.                                 }
  1858. -                               QSortT(list->Begin(), list->Length(), DropDownListStringItem::NatSortFunc);
  1859. +                               std::sort(list->Begin(), list->End(), DropDownListStringItem::NatSortFunc);
  1860.  
  1861.                                 /* Append custom currency at the end */
  1862.                                 *list->Append() = new DropDownListItem(-1, false); // separator line
  1863. @@ -241,7 +241,7 @@ struct GameOptionsWindow : Window {
  1864.                                         int result = _nb_orig_names + i;
  1865.                                         *list->Append() = new DropDownListStringItem(_grf_names[i], result, enabled_item != result && enabled_item >= 0);
  1866.                                 }
  1867. -                               QSortT(list->Begin(), list->Length(), DropDownListStringItem::NatSortFunc);
  1868. +                               std::sort(list->Begin(), list->End(), DropDownListStringItem::NatSortFunc);
  1869.  
  1870.                                 int newgrf_size = list->Length();
  1871.                                 /* Insert newgrf_names at the top of the list */
  1872. @@ -254,7 +254,7 @@ struct GameOptionsWindow : Window {
  1873.                                 for (int i = 0; i < _nb_orig_names; i++) {
  1874.                                         *list->Append() = new DropDownListStringItem(STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + i, i, enabled_item != i && enabled_item >= 0);
  1875.                                 }
  1876. -                               QSortT(list->Begin() + newgrf_size, list->Length() - newgrf_size, DropDownListStringItem::NatSortFunc);
  1877. +                               std::sort(list->Begin() + newgrf_size, list->End(), DropDownListStringItem::NatSortFunc);
  1878.                                 break;
  1879.                         }
  1880.  
  1881. @@ -274,7 +274,7 @@ struct GameOptionsWindow : Window {
  1882.                                         if (&_languages[i] == _current_language) *selected_index = i;
  1883.                                         *list->Append() = new DropDownListStringItem(SPECSTR_LANGUAGE_START + i, i, false);
  1884.                                 }
  1885. -                               QSortT(list->Begin(), list->Length(), DropDownListStringItem::NatSortFunc);
  1886. +                               std::sort(list->Begin(), list->End(), DropDownListStringItem::NatSortFunc);
  1887.                                 break;
  1888.                         }
  1889.  
  1890. diff --git a/src/signs_gui.cpp b/src/signs_gui.cpp
  1891. index 020ee1b6e..d7b396624 100644
  1892. --- a/src/signs_gui.cpp
  1893. +++ b/src/signs_gui.cpp
  1894. @@ -72,23 +72,22 @@ struct SignList {
  1895.         }
  1896.  
  1897.         /** Sort signs by their name */
  1898. -       static int CDECL SignNameSorter(const Sign * const *a, const Sign * const *b)
  1899. +       static bool CDECL SignNameSorter(const Sign * const &a, const Sign * const &b)
  1900.         {
  1901.                 static char buf_cache[64];
  1902.                 char buf[64];
  1903.  
  1904. -               SetDParam(0, (*a)->index);
  1905. +               SetDParam(0, a->index);
  1906.                 GetString(buf, STR_SIGN_NAME, lastof(buf));
  1907.  
  1908. -               if (*b != last_sign) {
  1909. -                       last_sign = *b;
  1910. -                       SetDParam(0, (*b)->index);
  1911. +               if (b != last_sign) {
  1912. +                       last_sign = b;
  1913. +                       SetDParam(0, b->index);
  1914.                         GetString(buf_cache, STR_SIGN_NAME, lastof(buf_cache));
  1915.                 }
  1916.  
  1917.                 int r = strnatcmp(buf, buf_cache); // Sort by name (natural sorting).
  1918. -
  1919. -               return r != 0 ? r : ((*a)->index - (*b)->index);
  1920. +               return r != 0 ? r < 0 : (a->index < b->index);
  1921.         }
  1922.  
  1923.         void SortSignsList()
  1924. diff --git a/src/sortlist_type.h b/src/sortlist_type.h
  1925. index 1a30c3b1a..68924f446 100644
  1926. --- a/src/sortlist_type.h
  1927. +++ b/src/sortlist_type.h
  1928. @@ -12,9 +12,11 @@
  1929.  #ifndef SORTLIST_TYPE_H
  1930.  #define SORTLIST_TYPE_H
  1931.  
  1932. +#include <algorithm>
  1933. +#include <functional>
  1934. +
  1935.  #include "core/enum_type.hpp"
  1936.  #include "core/bitmath_func.hpp"
  1937. -#include "core/sort_func.hpp"
  1938.  #include "core/smallvec_type.hpp"
  1939.  #include "date_type.h"
  1940.  
  1941. @@ -41,6 +43,19 @@ struct Filtering {
  1942.         byte criteria; ///< Filtering criteria
  1943.  };
  1944.  
  1945. +template <typename F, typename T>
  1946. +struct BinNegator {
  1947. +       F *f;
  1948. +       BinNegator(F *f) : f(f) {}
  1949. +
  1950. +       bool operator()(const T &a, const T &b) { return !f(a, b); };
  1951. +
  1952. +       static BinNegator<F, T> Make(F *f)
  1953. +       {
  1954. +               return BinNegator<F, T>(f);
  1955. +       }
  1956. +};
  1957. +
  1958.  /**
  1959.   * List template of 'things' \p T to sort in a GUI.
  1960.   * @tparam T Type of data stored in the list to represent each item.
  1961. @@ -49,8 +64,8 @@ struct Filtering {
  1962.  template <typename T, typename F = const char*>
  1963.  class GUIList : public SmallVector<T, 32> {
  1964.  public:
  1965. -       typedef int CDECL SortFunction(const T*, const T*); ///< Signature of sort function.
  1966. -       typedef bool CDECL FilterFunction(const T*, F);     ///< Signature of filter function.
  1967. +       typedef bool CDECL SortFunction(const T&, const T&); ///< Signature of sort function.
  1968. +       typedef bool CDECL FilterFunction(const T*, F);      ///< Signature of filter function.
  1969.  
  1970.  protected:
  1971.         SortFunction * const *sort_func_list;     ///< the sort criteria functions
  1972. @@ -265,16 +280,12 @@ public:
  1973.                 /* Do not sort when the list is not sortable */
  1974.                 if (!this->IsSortable()) return false;
  1975.  
  1976. -               const bool desc = (this->flags & VL_DESC) != 0;
  1977. -
  1978.                 if (this->flags & VL_FIRST_SORT) {
  1979.                         CLRBITS(this->flags, VL_FIRST_SORT);
  1980. -
  1981. -                       QSortT(this->data, this->items, compare, desc);
  1982. -                       return true;
  1983.                 }
  1984.  
  1985. -               GSortT(this->data, this->items, compare, desc);
  1986. +               const bool desc = (this->flags & VL_DESC) != 0;
  1987. +               std::sort(this->Begin(), this->End(), desc ? BinNegator<SortFunction, T>::Make(compare) : compare);
  1988.                 return true;
  1989.         }
  1990.  
  1991. diff --git a/src/station_gui.cpp b/src/station_gui.cpp
  1992. index 7399fe006..5df763ee4 100644
  1993. --- a/src/station_gui.cpp
  1994. +++ b/src/station_gui.cpp
  1995. @@ -209,85 +209,80 @@ protected:
  1996.         }
  1997.  
  1998.         /** Sort stations by their name */
  1999. -       static int CDECL StationNameSorter(const Station * const *a, const Station * const *b)
  2000. +       static bool CDECL StationNameSorter(const Station * const &a, const Station * const &b)
  2001.         {
  2002.                 static char buf_cache[64];
  2003.                 char buf[64];
  2004.  
  2005. -               SetDParam(0, (*a)->index);
  2006. +               SetDParam(0, a->index);
  2007.                 GetString(buf, STR_STATION_NAME, lastof(buf));
  2008.  
  2009. -               if (*b != last_station) {
  2010. -                       last_station = *b;
  2011. -                       SetDParam(0, (*b)->index);
  2012. +               if (b != last_station) {
  2013. +                       last_station = b;
  2014. +                       SetDParam(0, b->index);
  2015.                         GetString(buf_cache, STR_STATION_NAME, lastof(buf_cache));
  2016.                 }
  2017.  
  2018.                 int r = strnatcmp(buf, buf_cache); // Sort by name (natural sorting).
  2019. -               if (r == 0) return (*a)->index - (*b)->index;
  2020. -               return r;
  2021. +               return r == 0 ? a->index < b->index : r < 0;
  2022.         }
  2023.  
  2024.         /** Sort stations by their type */
  2025. -       static int CDECL StationTypeSorter(const Station * const *a, const Station * const *b)
  2026. +       static bool CDECL StationTypeSorter(const Station * const &a, const Station * const &b)
  2027.         {
  2028. -               return (*a)->facilities - (*b)->facilities;
  2029. +               return a->facilities - b->facilities;
  2030.         }
  2031.  
  2032.         /** Sort stations by their waiting cargo */
  2033. -       static int CDECL StationWaitingTotalSorter(const Station * const *a, const Station * const *b)
  2034. +       static bool CDECL StationWaitingTotalSorter(const Station * const &a, const Station * const &b)
  2035.         {
  2036.                 int diff = 0;
  2037.  
  2038.                 CargoID j;
  2039.                 FOR_EACH_SET_CARGO_ID(j, cargo_filter) {
  2040. -                       diff += (*a)->goods[j].cargo.TotalCount() - (*b)->goods[j].cargo.TotalCount();
  2041. +                       diff += a->goods[j].cargo.TotalCount() - b->goods[j].cargo.TotalCount();
  2042.                 }
  2043. -
  2044. -               return diff;
  2045. +               return diff < 0;
  2046.         }
  2047.  
  2048.         /** Sort stations by their available waiting cargo */
  2049. -       static int CDECL StationWaitingAvailableSorter(const Station * const *a, const Station * const *b)
  2050. +       static bool CDECL StationWaitingAvailableSorter(const Station * const &a, const Station * const &b)
  2051.         {
  2052.                 int diff = 0;
  2053.  
  2054.                 CargoID j;
  2055.                 FOR_EACH_SET_CARGO_ID(j, cargo_filter) {
  2056. -                       diff += (*a)->goods[j].cargo.AvailableCount() - (*b)->goods[j].cargo.AvailableCount();
  2057. +                       diff += a->goods[j].cargo.AvailableCount() - b->goods[j].cargo.AvailableCount();
  2058.                 }
  2059. -
  2060. -               return diff;
  2061. +               return diff < 0;
  2062.         }
  2063.  
  2064.         /** Sort stations by their rating */
  2065. -       static int CDECL StationRatingMaxSorter(const Station * const *a, const Station * const *b)
  2066. +       static bool CDECL StationRatingMaxSorter(const Station * const &a, const Station * const &b)
  2067.         {
  2068.                 byte maxr1 = 0;
  2069.                 byte maxr2 = 0;
  2070.  
  2071.                 CargoID j;
  2072.                 FOR_EACH_SET_CARGO_ID(j, cargo_filter) {
  2073. -                       if ((*a)->goods[j].HasRating()) maxr1 = max(maxr1, (*a)->goods[j].rating);
  2074. -                       if ((*b)->goods[j].HasRating()) maxr2 = max(maxr2, (*b)->goods[j].rating);
  2075. +                       if (a->goods[j].HasRating()) maxr1 = max(maxr1, a->goods[j].rating);
  2076. +                       if (b->goods[j].HasRating()) maxr2 = max(maxr2, b->goods[j].rating);
  2077.                 }
  2078. -
  2079. -               return maxr1 - maxr2;
  2080. +               return maxr1 < maxr2;
  2081.         }
  2082.  
  2083.         /** Sort stations by their rating */
  2084. -       static int CDECL StationRatingMinSorter(const Station * const *a, const Station * const *b)
  2085. +       static bool CDECL StationRatingMinSorter(const Station * const &a, const Station * const &b)
  2086.         {
  2087.                 byte minr1 = 255;
  2088.                 byte minr2 = 255;
  2089.  
  2090.                 for (CargoID j = 0; j < NUM_CARGO; j++) {
  2091.                         if (!HasBit(cargo_filter, j)) continue;
  2092. -                       if ((*a)->goods[j].HasRating()) minr1 = min(minr1, (*a)->goods[j].rating);
  2093. -                       if ((*b)->goods[j].HasRating()) minr2 = min(minr2, (*b)->goods[j].rating);
  2094. +                       if (a->goods[j].HasRating()) minr1 = min(minr1, a->goods[j].rating);
  2095. +                       if (b->goods[j].HasRating()) minr2 = min(minr2, b->goods[j].rating);
  2096.                 }
  2097. -
  2098. -               return -(minr1 - minr2);
  2099. +               return !(minr1 < minr2);
  2100.         }
  2101.  
  2102.         /** Sort the stations list */
  2103. diff --git a/src/story_gui.cpp b/src/story_gui.cpp
  2104. index 003843310..2ee00225c 100644
  2105. --- a/src/story_gui.cpp
  2106. +++ b/src/story_gui.cpp
  2107. @@ -69,9 +69,9 @@ protected:
  2108.         }
  2109.  
  2110.         /** Sort story pages by order value. */
  2111. -       static int CDECL PageOrderSorter(const StoryPage * const *a, const StoryPage * const *b)
  2112. +       static bool CDECL PageOrderSorter(const StoryPage * const &a, const StoryPage * const &b)
  2113.         {
  2114. -               return (*a)->sort_value - (*b)->sort_value;
  2115. +               return a->sort_value < b->sort_value;
  2116.         }
  2117.  
  2118.         /** (Re)Build story page element list. */
  2119. @@ -98,9 +98,9 @@ protected:
  2120.         }
  2121.  
  2122.         /** Sort story page elements by order value. */
  2123. -       static int CDECL PageElementOrderSorter(const StoryPageElement * const *a, const StoryPageElement * const *b)
  2124. +       static bool CDECL PageElementOrderSorter(const StoryPageElement * const &a, const StoryPageElement * const &b)
  2125.         {
  2126. -               return (*a)->sort_value - (*b)->sort_value;
  2127. +               return a->sort_value < b->sort_value;
  2128.         }
  2129.  
  2130.         /*
  2131. diff --git a/src/strings.cpp b/src/strings.cpp
  2132. index 1c539d934..476a9095d 100644
  2133. --- a/src/strings.cpp
  2134. +++ b/src/strings.cpp
  2135. @@ -1857,14 +1857,14 @@ const char *GetCurrentLocale(const char *param)
  2136.  const char *GetCurrentLocale(const char *param);
  2137.  #endif /* !(defined(WIN32) || defined(__APPLE__)) */
  2138.  
  2139. -int CDECL StringIDSorter(const StringID *a, const StringID *b)
  2140. +bool CDECL StringIDSorter(const StringID &a, const StringID &b)
  2141.  {
  2142.         char stra[512];
  2143.         char strb[512];
  2144. -       GetString(stra, *a, lastof(stra));
  2145. -       GetString(strb, *b, lastof(strb));
  2146. +       GetString(stra, a, lastof(stra));
  2147. +       GetString(strb, b, lastof(strb));
  2148.  
  2149. -       return strnatcmp(stra, strb);
  2150. +       return strnatcmp(stra, strb) < 0;
  2151.  }
  2152.  
  2153.  /**
  2154. diff --git a/src/strings_func.h b/src/strings_func.h
  2155. index 0da711bc4..4d17a5462 100644
  2156. --- a/src/strings_func.h
  2157. +++ b/src/strings_func.h
  2158. @@ -238,7 +238,7 @@ extern TextDirection _current_text_dir; ///< Text direction of the currently sel
  2159.  void InitializeLanguagePacks();
  2160.  const char *GetCurrentLanguageIsoCode();
  2161.  
  2162. -int CDECL StringIDSorter(const StringID *a, const StringID *b);
  2163. +bool CDECL StringIDSorter(const StringID &a, const StringID &b);
  2164.  
  2165.  /**
  2166.   * A searcher for missing glyphs.
  2167. diff --git a/src/timetable_cmd.cpp b/src/timetable_cmd.cpp
  2168. index 29986c353..274d4a68e 100644
  2169. --- a/src/timetable_cmd.cpp
  2170. +++ b/src/timetable_cmd.cpp
  2171. @@ -16,7 +16,6 @@
  2172.  #include "window_func.h"
  2173.  #include "vehicle_base.h"
  2174.  #include "cmd_helper.h"
  2175. -#include "core/sort_func.hpp"
  2176.  
  2177.  #include "table/strings.h"
  2178.  
  2179. @@ -220,11 +219,8 @@ CommandCost CmdSetVehicleOnTime(TileIndex tile, DoCommandFlag flags, uint32 p1,
  2180.   * @param bp Second Vehicle pointer.
  2181.   * @return Comparison value.
  2182.   */
  2183. -static int CDECL VehicleTimetableSorter(Vehicle * const *ap, Vehicle * const *bp)
  2184. +static bool CDECL VehicleTimetableSorter(Vehicle * const &a, Vehicle * const &b)
  2185.  {
  2186. -       const Vehicle *a = *ap;
  2187. -       const Vehicle *b = *bp;
  2188. -
  2189.         VehicleOrderID a_order = a->cur_real_order_index;
  2190.         VehicleOrderID b_order = b->cur_real_order_index;
  2191.         int j = (int)b_order - (int)a_order;
  2192. @@ -242,15 +238,15 @@ static int CDECL VehicleTimetableSorter(Vehicle * const *ap, Vehicle * const *bp
  2193.  
  2194.         /* First check the order index that accounted for loading, then just the raw one. */
  2195.         int i = (int)b_order - (int)a_order;
  2196. -       if (i != 0) return i;
  2197. -       if (j != 0) return j;
  2198. +       if (i != 0) return i < 0;
  2199. +       if (j != 0) return j < 0;
  2200.  
  2201.         /* Look at the time we spent in this order; the higher, the closer to its destination. */
  2202.         i = b->current_order_time - a->current_order_time;
  2203. -       if (i != 0) return i;
  2204. +       if (i != 0) return i < 0;
  2205.  
  2206.         /* If all else is equal, use some unique index to sort it the same way. */
  2207. -       return b->unitnumber - a->unitnumber;
  2208. +       return b->unitnumber < a->unitnumber;
  2209.  }
  2210.  
  2211.  /**
  2212. @@ -295,7 +291,7 @@ CommandCost CmdSetTimetableStart(TileIndex tile, DoCommandFlag flags, uint32 p1,
  2213.                 int num_vehs = vehs.Length();
  2214.  
  2215.                 if (num_vehs >= 2) {
  2216. -                       QSortT(vehs.Begin(), vehs.Length(), &VehicleTimetableSorter);
  2217. +                       std::sort(vehs.Begin(), vehs.End(), &VehicleTimetableSorter);
  2218.                 }
  2219.  
  2220.                 int base = vehs.FindIndex(v);
  2221. diff --git a/src/town_gui.cpp b/src/town_gui.cpp
  2222. index 142966b69..e7c82be53 100644
  2223. --- a/src/town_gui.cpp
  2224. +++ b/src/town_gui.cpp
  2225. @@ -666,11 +666,11 @@ private:
  2226.         }
  2227.  
  2228.         /** Sort by town name */
  2229. -       static int CDECL TownNameSorter(const Town * const *a, const Town * const *b)
  2230. +       static bool CDECL TownNameSorter(const Town * const &a, const Town * const &b)
  2231.         {
  2232.                 static char buf_cache[64];
  2233. -               const Town *ta = *a;
  2234. -               const Town *tb = *b;
  2235. +               const Town *ta = a;
  2236. +               const Town *tb = b;
  2237.                 char buf[64];
  2238.  
  2239.                 SetDParam(0, ta->index);
  2240. @@ -685,35 +685,35 @@ private:
  2241.                         GetString(buf_cache, STR_TOWN_NAME, lastof(buf_cache));
  2242.                 }
  2243.  
  2244. -               return strnatcmp(buf, buf_cache); // Sort by name (natural sorting).
  2245. +               return strnatcmp(buf, buf_cache) < 0; // Sort by name (natural sorting).
  2246.         }
  2247.  
  2248.         /** Sort by population (default descending, as big towns are of the most interest). */
  2249. -       static int CDECL TownPopulationSorter(const Town * const *a, const Town * const *b)
  2250. +       static bool CDECL TownPopulationSorter(const Town * const &a, const Town * const &b)
  2251.         {
  2252. -               uint32 a_population = (*a)->cache.population;
  2253. -               uint32 b_population = (*b)->cache.population;
  2254. +               uint32 a_population = a->cache.population;
  2255. +               uint32 b_population = b->cache.population;
  2256.                 if (a_population == b_population) return TownDirectoryWindow::TownNameSorter(a, b);
  2257. -               return (a_population < b_population) ? -1 : 1;
  2258. +               return a_population < b_population;
  2259.         }
  2260.  
  2261.         /** Sort by town rating */
  2262. -       static int CDECL TownRatingSorter(const Town * const *a, const Town * const *b)
  2263. +       static bool CDECL TownRatingSorter(const Town * const &a, const Town * const &b)
  2264.         {
  2265. -               int before = TownDirectoryWindow::last_sorting.order ? 1 : -1; // Value to get 'a' before 'b'.
  2266. +               bool before = !TownDirectoryWindow::last_sorting.order; // Value to get 'a' before 'b'.
  2267.  
  2268.                 /* Towns without rating are always after towns with rating. */
  2269. -               if (HasBit((*a)->have_ratings, _local_company)) {
  2270. -                       if (HasBit((*b)->have_ratings, _local_company)) {
  2271. -                               int16 a_rating = (*a)->ratings[_local_company];
  2272. -                               int16 b_rating = (*b)->ratings[_local_company];
  2273. +               if (HasBit(a->have_ratings, _local_company)) {
  2274. +                       if (HasBit(b->have_ratings, _local_company)) {
  2275. +                               int16 a_rating = a->ratings[_local_company];
  2276. +                               int16 b_rating = b->ratings[_local_company];
  2277.                                 if (a_rating == b_rating) return TownDirectoryWindow::TownNameSorter(a, b);
  2278. -                               return (a_rating < b_rating) ? -1 : 1;
  2279. +                               return a_rating < b_rating;
  2280.                         }
  2281.                         return before;
  2282.                 }
  2283. -               if (HasBit((*b)->have_ratings, _local_company)) return -before;
  2284. -               return -before * TownDirectoryWindow::TownNameSorter(a, b); // Sort unrated towns always on ascending town name.
  2285. +               if (HasBit(b->have_ratings, _local_company)) return !before;
  2286. +               return !before && TownDirectoryWindow::TownNameSorter(a, b); // Sort unrated towns always on ascending town name.
  2287.         }
  2288.  
  2289.  public:
  2290. diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp
  2291. index b9c24ddd0..9d2fe7b54 100644
  2292. --- a/src/vehicle_gui.cpp
  2293. +++ b/src/vehicle_gui.cpp
  2294. @@ -194,7 +194,7 @@ void BaseVehicleListWindow::SortVehicleList()
  2295.  void DepotSortList(VehicleList *list)
  2296.  {
  2297.         if (list->Length() < 2) return;
  2298. -       QSortT(list->Begin(), list->Length(), &VehicleNumberSorter);
  2299. +       std::sort(list->Begin(), list->End(), &VehicleNumberSorter);
  2300.  }
  2301.  
  2302.  /** draw the vehicle profit button in the vehicle list window. */
  2303. @@ -1085,62 +1085,61 @@ StringID GetCargoSubtypeText(const Vehicle *v)
  2304.  }
  2305.  
  2306.  /** Sort vehicles by their number */
  2307. -static int CDECL VehicleNumberSorter(const Vehicle * const *a, const Vehicle * const *b)
  2308. +static bool CDECL VehicleNumberSorter(const Vehicle * const &a, const Vehicle * const &b)
  2309.  {
  2310. -       return (*a)->unitnumber - (*b)->unitnumber;
  2311. +       return a->unitnumber < b->unitnumber;
  2312.  }
  2313.  
  2314.  /** Sort vehicles by their name */
  2315. -static int CDECL VehicleNameSorter(const Vehicle * const *a, const Vehicle * const *b)
  2316. +static bool CDECL VehicleNameSorter(const Vehicle * const &a, const Vehicle * const &b)
  2317.  {
  2318.         static char last_name[2][64];
  2319.  
  2320. -       if (*a != _last_vehicle[0]) {
  2321. -               _last_vehicle[0] = *a;
  2322. -               SetDParam(0, (*a)->index);
  2323. +       if (a != _last_vehicle[0]) {
  2324. +               _last_vehicle[0] = a;
  2325. +               SetDParam(0, a->index);
  2326.                 GetString(last_name[0], STR_VEHICLE_NAME, lastof(last_name[0]));
  2327.         }
  2328.  
  2329. -       if (*b != _last_vehicle[1]) {
  2330. -               _last_vehicle[1] = *b;
  2331. -               SetDParam(0, (*b)->index);
  2332. +       if (b != _last_vehicle[1]) {
  2333. +               _last_vehicle[1] = b;
  2334. +               SetDParam(0, b->index);
  2335.                 GetString(last_name[1], STR_VEHICLE_NAME, lastof(last_name[1]));
  2336.         }
  2337.  
  2338.         int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
  2339. -       return (r != 0) ? r : VehicleNumberSorter(a, b);
  2340. +       return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
  2341.  }
  2342.  
  2343.  /** Sort vehicles by their age */
  2344. -static int CDECL VehicleAgeSorter(const Vehicle * const *a, const Vehicle * const *b)
  2345. +static bool CDECL VehicleAgeSorter(const Vehicle * const &a, const Vehicle * const &b)
  2346.  {
  2347. -       int r = (*a)->age - (*b)->age;
  2348. -       return (r != 0) ? r : VehicleNumberSorter(a, b);
  2349. +       return a->age != b->age ? a->age < b->age : VehicleNumberSorter(a, b);
  2350.  }
  2351.  
  2352.  /** Sort vehicles by this year profit */
  2353. -static int CDECL VehicleProfitThisYearSorter(const Vehicle * const *a, const Vehicle * const *b)
  2354. +static bool CDECL VehicleProfitThisYearSorter(const Vehicle * const &a, const Vehicle * const &b)
  2355.  {
  2356. -       int r = ClampToI32((*a)->GetDisplayProfitThisYear() - (*b)->GetDisplayProfitThisYear());
  2357. -       return (r != 0) ? r : VehicleNumberSorter(a, b);
  2358. +       int r = ClampToI32(a->GetDisplayProfitThisYear() - b->GetDisplayProfitThisYear());
  2359. +       return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
  2360.  }
  2361.  
  2362.  /** Sort vehicles by last year profit */
  2363. -static int CDECL VehicleProfitLastYearSorter(const Vehicle * const *a, const Vehicle * const *b)
  2364. +static bool CDECL VehicleProfitLastYearSorter(const Vehicle * const &a, const Vehicle * const &b)
  2365.  {
  2366. -       int r = ClampToI32((*a)->GetDisplayProfitLastYear() - (*b)->GetDisplayProfitLastYear());
  2367. -       return (r != 0) ? r : VehicleNumberSorter(a, b);
  2368. +       int r = ClampToI32(a->GetDisplayProfitLastYear() - b->GetDisplayProfitLastYear());
  2369. +       return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
  2370.  }
  2371.  
  2372.  /** Sort vehicles by their cargo */
  2373. -static int CDECL VehicleCargoSorter(const Vehicle * const *a, const Vehicle * const *b)
  2374. +static bool CDECL VehicleCargoSorter(const Vehicle * const &a, const Vehicle * const &b)
  2375.  {
  2376.         const Vehicle *v;
  2377.         CargoArray diff;
  2378.  
  2379.         /* Append the cargo of the connected waggons */
  2380. -       for (v = *a; v != NULL; v = v->Next()) diff[v->cargo_type] += v->cargo_cap;
  2381. -       for (v = *b; v != NULL; v = v->Next()) diff[v->cargo_type] -= v->cargo_cap;
  2382. +       for (v = a; v != NULL; v = v->Next()) diff[v->cargo_type] += v->cargo_cap;
  2383. +       for (v = b; v != NULL; v = v->Next()) diff[v->cargo_type] -= v->cargo_cap;
  2384.  
  2385.         int r = 0;
  2386.         for (CargoID i = 0; i < NUM_CARGO; i++) {
  2387. @@ -1148,62 +1147,62 @@ static int CDECL VehicleCargoSorter(const Vehicle * const *a, const Vehicle * co
  2388.                 if (r != 0) break;
  2389.         }
  2390.  
  2391. -       return (r != 0) ? r : VehicleNumberSorter(a, b);
  2392. +       return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
  2393.  }
  2394.  
  2395.  /** Sort vehicles by their reliability */
  2396. -static int CDECL VehicleReliabilitySorter(const Vehicle * const *a, const Vehicle * const *b)
  2397. +static bool CDECL VehicleReliabilitySorter(const Vehicle * const &a, const Vehicle * const &b)
  2398.  {
  2399. -       int r = (*a)->reliability - (*b)->reliability;
  2400. -       return (r != 0) ? r : VehicleNumberSorter(a, b);
  2401. +       int r = a->reliability - b->reliability;
  2402. +       return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
  2403.  }
  2404.  
  2405.  /** Sort vehicles by their max speed */
  2406. -static int CDECL VehicleMaxSpeedSorter(const Vehicle * const *a, const Vehicle * const *b)
  2407. +static bool CDECL VehicleMaxSpeedSorter(const Vehicle * const &a, const Vehicle * const &b)
  2408.  {
  2409. -       int r = (*a)->vcache.cached_max_speed - (*b)->vcache.cached_max_speed;
  2410. -       return (r != 0) ? r : VehicleNumberSorter(a, b);
  2411. +       int r = a->vcache.cached_max_speed - b->vcache.cached_max_speed;
  2412. +       return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
  2413.  }
  2414.  
  2415.  /** Sort vehicles by model */
  2416. -static int CDECL VehicleModelSorter(const Vehicle * const *a, const Vehicle * const *b)
  2417. +static bool CDECL VehicleModelSorter(const Vehicle * const &a, const Vehicle * const &b)
  2418.  {
  2419. -       int r = (*a)->engine_type - (*b)->engine_type;
  2420. -       return (r != 0) ? r : VehicleNumberSorter(a, b);
  2421. +       int r = a->engine_type - b->engine_type;
  2422. +       return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
  2423.  }
  2424.  
  2425.  /** Sort vehicles by their value */
  2426. -static int CDECL VehicleValueSorter(const Vehicle * const *a, const Vehicle * const *b)
  2427. +static bool CDECL VehicleValueSorter(const Vehicle * const &a, const Vehicle * const &b)
  2428.  {
  2429.         const Vehicle *u;
  2430.         Money diff = 0;
  2431.  
  2432. -       for (u = *a; u != NULL; u = u->Next()) diff += u->value;
  2433. -       for (u = *b; u != NULL; u = u->Next()) diff -= u->value;
  2434. +       for (u = a; u != NULL; u = u->Next()) diff += u->value;
  2435. +       for (u = b; u != NULL; u = u->Next()) diff -= u->value;
  2436.  
  2437.         int r = ClampToI32(diff);
  2438. -       return (r != 0) ? r : VehicleNumberSorter(a, b);
  2439. +       return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
  2440.  }
  2441.  
  2442.  /** Sort vehicles by their length */
  2443. -static int CDECL VehicleLengthSorter(const Vehicle * const *a, const Vehicle * const *b)
  2444. +static bool CDECL VehicleLengthSorter(const Vehicle * const &a, const Vehicle * const &b)
  2445.  {
  2446. -       int r = (*a)->GetGroundVehicleCache()->cached_total_length - (*b)->GetGroundVehicleCache()->cached_total_length;
  2447. -       return (r != 0) ? r : VehicleNumberSorter(a, b);
  2448. +       int r = a->GetGroundVehicleCache()->cached_total_length - b->GetGroundVehicleCache()->cached_total_length;
  2449. +       return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
  2450.  }
  2451.  
  2452.  /** Sort vehicles by the time they can still live */
  2453. -static int CDECL VehicleTimeToLiveSorter(const Vehicle * const *a, const Vehicle * const *b)
  2454. +static bool CDECL VehicleTimeToLiveSorter(const Vehicle * const &a, const Vehicle * const &b)
  2455.  {
  2456. -       int r = ClampToI32(((*a)->max_age - (*a)->age) - ((*b)->max_age - (*b)->age));
  2457. -       return (r != 0) ? r : VehicleNumberSorter(a, b);
  2458. +       int r = ClampToI32((a->max_age - a->age) - (b->max_age - b->age));
  2459. +       return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
  2460.  }
  2461.  
  2462.  /** Sort vehicles by the timetable delay */
  2463. -static int CDECL VehicleTimetableDelaySorter(const Vehicle * const *a, const Vehicle * const *b)
  2464. +static bool CDECL VehicleTimetableDelaySorter(const Vehicle * const &a, const Vehicle * const &b)
  2465.  {
  2466. -       int r = (*a)->lateness_counter - (*b)->lateness_counter;
  2467. -       return (r != 0) ? r : VehicleNumberSorter(a, b);
  2468. +       int r = a->lateness_counter - b->lateness_counter;
  2469. +       return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
  2470.  }
  2471.  
  2472.  void InitializeGUI()
  2473. diff --git a/src/video/cocoa/cocoa_v.mm b/src/video/cocoa/cocoa_v.mm
  2474. index 4df7cb003..4618a45b2 100644
  2475. --- a/src/video/cocoa/cocoa_v.mm
  2476. +++ b/src/video/cocoa/cocoa_v.mm
  2477. @@ -36,6 +36,7 @@
  2478.  #include "../../window_func.h"
  2479.  #include "../../window_gui.h"
  2480.  
  2481. +#import <sort>
  2482.  #import <sys/param.h> /* for MAXPATHLEN */
  2483.  
  2484.  /**
  2485. @@ -278,7 +279,7 @@ uint QZ_ListModes(OTTD_Point *modes, uint max_modes, CGDirectDisplayID display_i
  2486.         }
  2487.  
  2488.         /* Sort list smallest to largest */
  2489. -       QSortT(modes, count, &ModeSorter);
  2490. +       std::sort(modes, modes + count, &ModeSorter);
  2491.  
  2492.         return count;
  2493.  }
  2494. diff --git a/src/widgets/dropdown.cpp b/src/widgets/dropdown.cpp
  2495. index d4c229cb1..7f5fce130 100644
  2496. --- a/src/widgets/dropdown.cpp
  2497. +++ b/src/widgets/dropdown.cpp
  2498. @@ -50,12 +50,12 @@ void DropDownListStringItem::Draw(int left, int right, int top, int bottom, bool
  2499.   * @return true if \a first precedes \a second.
  2500.   * @warning All items in the list need to be derivates of DropDownListStringItem.
  2501.   */
  2502. -/* static */ int DropDownListStringItem::NatSortFunc(const DropDownListItem * const *first, const DropDownListItem * const * second)
  2503. +/* static */ bool DropDownListStringItem::NatSortFunc(const DropDownListItem * const &first, const DropDownListItem * const &second)
  2504.  {
  2505.         char buffer1[512], buffer2[512];
  2506. -       GetString(buffer1, static_cast<const DropDownListStringItem*>(*first)->String(), lastof(buffer1));
  2507. -       GetString(buffer2, static_cast<const DropDownListStringItem*>(*second)->String(), lastof(buffer2));
  2508. -       return strnatcmp(buffer1, buffer2);
  2509. +       GetString(buffer1, static_cast<const DropDownListStringItem*>(first)->String(), lastof(buffer1));
  2510. +       GetString(buffer2, static_cast<const DropDownListStringItem*>(second)->String(), lastof(buffer2));
  2511. +       return strnatcmp(buffer1, buffer2) < 0;
  2512.  }
  2513.  
  2514.  StringID DropDownListParamStringItem::String() const
  2515. diff --git a/src/widgets/dropdown_type.h b/src/widgets/dropdown_type.h
  2516. index b65d4557b..e87ea1e85 100644
  2517. --- a/src/widgets/dropdown_type.h
  2518. +++ b/src/widgets/dropdown_type.h
  2519. @@ -50,7 +50,7 @@ public:
  2520.         virtual void Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const;
  2521.         virtual StringID String() const { return this->string; }
  2522.  
  2523. -       static int CDECL NatSortFunc(const DropDownListItem * const *first, const DropDownListItem * const *second);
  2524. +       static bool CDECL NatSortFunc(const DropDownListItem * const &first, const DropDownListItem * const &second);
  2525.  };
  2526.  
  2527.  /**
  2528. diff --git a/src/window.cpp b/src/window.cpp
  2529. index 8378f60f7..b901abc77 100644
  2530. --- a/src/window.cpp
  2531. +++ b/src/window.cpp
  2532. @@ -150,10 +150,10 @@ void WindowDesc::LoadFromConfig()
  2533.  /**
  2534.   * Sort WindowDesc by ini_key.
  2535.   */
  2536. -static int CDECL DescSorter(WindowDesc * const *a, WindowDesc * const *b)
  2537. +static bool CDECL DescSorter(WindowDesc * const &a, WindowDesc * const &b)
  2538.  {
  2539. -       if ((*a)->ini_key != NULL && (*b)->ini_key != NULL) return strcmp((*a)->ini_key, (*b)->ini_key);
  2540. -       return ((*b)->ini_key != NULL ? 1 : 0) - ((*a)->ini_key != NULL ? 1 : 0);
  2541. +       if (a->ini_key != NULL && b->ini_key != NULL) return strcmp(a->ini_key, b->ini_key) < 0;
  2542. +       return a->ini_key != NULL;
  2543.  }
  2544.  
  2545.  /**
  2546. @@ -162,7 +162,7 @@ static int CDECL DescSorter(WindowDesc * const *a, WindowDesc * const *b)
  2547.  void WindowDesc::SaveToConfig()
  2548.  {
  2549.         /* Sort the stuff to get a nice ini file on first write */
  2550. -       QSortT(_window_descs->Begin(), _window_descs->Length(), DescSorter);
  2551. +       std::sort(_window_descs->Begin(), _window_descs->End(), &DescSorter);
  2552.  
  2553.         IniFile *ini = new IniFile();
  2554.         ini->LoadFromDisk(_windows_file, NO_DIRECTORY);

Comments