tcomparerproject.lpr 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Generic types for NewPascal.org and FPC!
  2. // by Maciej Izak (hnb), 2014
  3. program TComparerProject;
  4. {$MODE DELPHI}
  5. {$APPTYPE CONSOLE}
  6. uses
  7. SysUtils, Generics.Collections, Generics.Defaults;
  8. type
  9. { TCustomer }
  10. TCustomer = record
  11. private
  12. FName: string;
  13. FMoney: Currency;
  14. public
  15. constructor Create(const Name: string; Money: Currency);
  16. property Name: string read FName write FName;
  17. property Money: Currency read FMoney write FMoney;
  18. function ToString: string;
  19. end;
  20. TCustomerComparer = class(TComparer<TCustomer>)
  21. function Compare(constref Left, Right: TCustomer): Integer; override;
  22. end;
  23. { TCustomer }
  24. constructor TCustomer.Create(const Name: string; Money: Currency);
  25. begin
  26. FName := Name;
  27. FMoney := Money;
  28. end;
  29. function TCustomer.ToString: string;
  30. begin
  31. Result := Format('Name: %s >>> Money: %m', [Name, Money]);
  32. end;
  33. // Ascending
  34. function TCustomerComparer.Compare(constref Left, Right: TCustomer): Integer;
  35. begin
  36. Result := TCompare.&String(Left.Name, Right.Name);
  37. if Result = 0 then
  38. Result := TCompare.Currency(Left.Money, Right.Money);
  39. end;
  40. // Descending
  41. function CustomerCompare(constref Left, Right: TCustomer): Integer;
  42. begin
  43. Result := TCompare.&String(Right.Name, Left.Name);
  44. if Result = 0 then
  45. Result := TCompare.Currency(Right.Money, Left.Money);
  46. end;
  47. var
  48. CustomersArray: TArray<TCustomer>;
  49. CustomersList: TList<TCustomer>;
  50. Comparer: TCustomerComparer;
  51. Customer: TCustomer;
  52. begin
  53. CustomersArray := TArray<TCustomer>.Create(
  54. TCustomer.Create('Derp', 2000),
  55. TCustomer.Create('Sheikh', 2000000000),
  56. TCustomer.Create('Derp', 1000),
  57. TCustomer.Create('Bill Gates', 1000000000));
  58. Comparer := TCustomerComparer.Create;
  59. Comparer._AddRef;
  60. // create TList with custom comparer
  61. CustomersList := TList<TCustomer>.Create(Comparer);
  62. CustomersList.AddRange(CustomersArray);
  63. WriteLn('CustomersList before sort:');
  64. for Customer in CustomersList do
  65. WriteLn(Customer.ToString);
  66. WriteLn;
  67. // default sort
  68. CustomersList.Sort; // will use TCustomerComparer (passed in the constructor)
  69. WriteLn('CustomersList after ascending sort (default with interface from constructor):');
  70. for Customer in CustomersList do
  71. WriteLn(Customer.ToString);
  72. WriteLn;
  73. // construct with simple function
  74. CustomersList.Sort(TComparer<TCustomer>.Construct(CustomerCompare));
  75. WriteLn('CustomersList after descending sort (by using construct with function)');
  76. WriteLn('CustomersList.Sort(TComparer<TCustomer>.Construct(CustomerCompare)):');
  77. for Customer in CustomersList do
  78. WriteLn(Customer.ToString);
  79. WriteLn;
  80. // construct with method
  81. CustomersList.Sort(TComparer<TCustomer>.Construct(Comparer.Compare));
  82. WriteLn('CustomersList after ascending sort (by using construct with method)');
  83. WriteLn('CustomersList.Sort(TComparer<TCustomer>.Construct(Comparer.Compare)):');
  84. for Customer in CustomersList do
  85. WriteLn(Customer.ToString);
  86. WriteLn;
  87. WriteLn('CustomersArray before sort:');
  88. for Customer in CustomersArray do
  89. WriteLn(Customer.ToString);
  90. WriteLn;
  91. // sort with interface
  92. TArrayHelper<TCustomer>.Sort(CustomersArray, TCustomerComparer.Create);
  93. WriteLn('CustomersArray after ascending sort (by using interfese - no construct)');
  94. WriteLn('TArrayHelper<TCustomer>.Sort(CustomersArray, TCustomerComparer.Create):');
  95. for Customer in CustomersArray do
  96. WriteLn(Customer.ToString);
  97. WriteLn;
  98. CustomersList.Free;
  99. Comparer._Release;
  100. ReadLn;
  101. end.