Unit unit1;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;
Type
Tform1 = Class (tform)
Button1: tbutton;
Procedure button1click (Sender: tobject );
End;
VaR
Form1: tform1;
Implementation
{$ R *. DFM}
Uses generics. Collections, generics. defaults;
Procedure tform1.button1click (Sender: tobject );
VaR
List: tlist <string>;
Num: integer;
B: Boolean;
Comparer: icomparer;
Begin
List: = tlist <string>. Create ();
List. addrange (['123', 'abc', 'abc']);
{The default search must be exactly the same}
If list. binarysearch ('abc', num) Then showmessage (inttostr (Num); {2}
{Here is a custom comparator. If the first character matches, it will be found}
Comparer: = tcomparer <string>. Construct (
Function (const S1, S2: string): integer
Begin
Result: = ord (S1 [1])-ord (s2 [1]);
End );
If list. binarysearch ('abc', num, comparer) Then showmessage (inttostr (Num); {1}
List. Free;
End;
End.