Generics Defaults TComparer (Delphi)
Description
This example demonstrates the usage of TComparer.
Four TComparer instances are used:
- A default comparer for String type: TComparer.Default.
- A binary comparer: TStringComparer.Ordinal.
- A custom TComparer comparer: TIntStringComparer.
- A String comparer constructed with an anonymous TComparison function.
Code
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils,
Generics.Defaults,
Generics.Collections,
AnsiStrings;
type
{ Declare a new custom comparer. }
TIntStringComparer = class(TComparer<String>)
public
function Compare(const Left, Right: String): Integer; override;
end;
{ TIntStringComparer }
function TIntStringComparer.Compare(const Left, Right: String): Integer;
var
LeftTerm, RightTerm: Integer;
begin
{ Transform the strings into integers and perform the comparison. }
try
LeftTerm := StrToInt(Left);
RightTerm := StrToInt(Right);
Result := LeftTerm - RightTerm;
except
on E: Exception do
begin
writeln('Not a number!');
Result := CompareStr(Left, Right);
end;
end;
end;
procedure FillListWithStrings(aList: TList<String>);
var
s: String;
begin
Writeln('list with strings:');
aList.Clear;
aList.AddRange(['didier', 'céline', 'louloute', 'john', 'didiot', 'coe']);
for s in aList do
Writeln(s);
end;
procedure FillListWithIntegers(aList: TList<String>);
var
s: String;
I: Integer;
begin
Writeln('list with integers:');
{ Populate the list with random numbers. }
for I := 0 to 5 do
aList.Add(IntToStr(Random(5)));
for s in aList do
Writeln(s);
end;
var
Cmp: IComparer<String>;
List: TList<String>;
A: Integer;
s: String;
begin
{ Use our custom comparer. }
Randomize;
Writeln('Choose the comparer:');
Writeln('1 = TComparer<String>.Default (default comparer for String type)');
Writeln('2 = TStringComparer.Ordinal (binary comparison)');
Writeln('3 = TIntStringComparer (custom TComparer<String> comparer)');
Writeln('4 = String comparer constructed with an anonymous TComparison<String> function');
Readln(A);
case A of
1: begin
// default comparer for String type
List := TList<String>.Create(TComparer<String>.Default);
FillListWithStrings(List);
List.Sort;
end;
2: begin
// binary comparison
List := TList<String>.Create(TStringComparer.Ordinal);
FillListWithStrings(List);
List.Sort;
end;
3: begin
// custom TComparer
Cmp := TIntStringComparer.Create;
List := TList<String>.Create(Cmp);
FillListWithIntegers(List);
List.Sort;
end;
4: begin
// anonymous TComparison<String> function
List := TList<String>.Create;
FillListWithStrings(List);
List.Sort(TComparer<String>.Construct(function(const Left, Right: String): integer
begin
if (Length(Left) < 2) or (Length(Right) < 2) then
exit(0);
result := ord(Left[2]) - ord(Right[2]);
end));
end;
else exit;
end;
Writeln('------------');
Writeln('Sorted list:');
for s in List do
Writeln(s);
{ Free resources. }
List.Free;
readln;
end.