File: TComparerProject.lpr

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (124 lines) | stat: -rw-r--r-- 3,510 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Generic types for FreeSparta.com and FreePascal!
// by Maciej Izak (hnb), 2014

program TComparerProject;

{$MODE DELPHI}
{$APPTYPE CONSOLE}

uses
  SysUtils, Generics.Collections, Generics.Defaults;

type

  { TCustomer }

  TCustomer = record
  private
    FName: string;
    FMoney: Currency;
  public
    constructor Create(const Name: string; Money: Currency);
    property Name: string read FName write FName;
    property Money: Currency read FMoney write FMoney;
    function ToString: string;
  end;

  TCustomerComparer = class(TComparer<TCustomer>)
    function Compare(constref Left, Right: TCustomer): Integer; override;
  end;

{ TCustomer }

constructor TCustomer.Create(const Name: string; Money: Currency);
begin
  FName := Name;
  FMoney := Money;
end;

function TCustomer.ToString: string;
begin
  Result := Format('Name: %s >>> Money: %m', [Name, Money]);
end;

// Ascending
function TCustomerComparer.Compare(constref Left, Right: TCustomer): Integer;
begin
  Result := TCompare.&String(Left.Name, Right.Name);
  if Result = 0 then
    Result := TCompare.Currency(Left.Money, Right.Money);
end;

// Descending
function CustomerCompare(constref Left, Right: TCustomer): Integer;
begin
  Result := TCompare.&String(Right.Name, Left.Name);
  if Result = 0 then
    Result := TCompare.Currency(Right.Money, Left.Money);
end;

var
  CustomersArray: TArray<TCustomer>;
  CustomersList: TList<TCustomer>;
  Comparer: TCustomerComparer;
  Customer: TCustomer;
begin
  CustomersArray := TArray<TCustomer>.Create(
    TCustomer.Create('Derp', 2000),
    TCustomer.Create('Sheikh', 2000000000),
    TCustomer.Create('Derp', 1000),
    TCustomer.Create('Bill Gates', 1000000000));

  Comparer := TCustomerComparer.Create;
  Comparer._AddRef;

  // create TList with custom comparer
  CustomersList := TList<TCustomer>.Create(Comparer);
  CustomersList.AddRange(CustomersArray);

  WriteLn('CustomersList before sort:');
  for Customer in CustomersList do
    WriteLn(Customer.ToString);
  WriteLn;

  // default sort
  CustomersList.Sort; // will use TCustomerComparer (passed in the constructor)
  WriteLn('CustomersList after ascending sort (default with interface from constructor):');
  for Customer in CustomersList do
    WriteLn(Customer.ToString);
  WriteLn;

  // construct with simple function
  CustomersList.Sort(TComparer<TCustomer>.Construct(CustomerCompare));
  WriteLn('CustomersList after descending sort (by using construct with function)');
  WriteLn('CustomersList.Sort(TComparer<TCustomer>.Construct(CustomerCompare)):');
  for Customer in CustomersList do
    WriteLn(Customer.ToString);
  WriteLn;

  // construct with method
  CustomersList.Sort(TComparer<TCustomer>.Construct(Comparer.Compare));
  WriteLn('CustomersList after ascending sort (by using construct with method)');
  WriteLn('CustomersList.Sort(TComparer<TCustomer>.Construct(Comparer.Compare)):');
  for Customer in CustomersList do
    WriteLn(Customer.ToString);
  WriteLn;

  WriteLn('CustomersArray before sort:');
  for Customer in CustomersArray do
    WriteLn(Customer.ToString);
  WriteLn;

  // sort with interface
  TArrayHelper<TCustomer>.Sort(CustomersArray, TCustomerComparer.Create);
  WriteLn('CustomersArray after ascending sort (by using interfese - no construct)');
  WriteLn('TArrayHelper<TCustomer>.Sort(CustomersArray, TCustomerComparer.Create):');
  for Customer in CustomersArray do
    WriteLn(Customer.ToString);
  WriteLn;

  CustomersList.Free;
  Comparer._Release;
  ReadLn;
end.