RAD Studio VCL Reference
|
Creates a new instance of TDelegatedComparer class.
constructor Create(const ACompare: TComparison<T>);
__fastcall TDelegatedComparer(const TComparison<T> ACompare);
Use Create method to create a new instance of TDelegatedComparer class. ACompare is a reference to a TComparison routine that will handle all comparison requests
Delphi Examples:
{ This example demonstrates the usage of TDelegatedComparer and anonymous methods in order to create a custom comparer. The example assumes two memos and a button are present on the form. } procedure TForm3.Button1Click(Sender: TObject); var List: TList<String>; Comparer: IComparer<String>; I: Integer; begin { Create a new delegated comparer. We will use anonymous methods } Comparer := TDelegatedComparer<String>.Create( { TComparison<String> } function(const Left, Right: String): Integer begin { Consider the strings as numbers and return } { a comparison between them } Result := StrToInt(Left) - StrToInt(Right); end); { Create a new list of strings } List := TList<String>.Create(Comparer); { Populate the list with numbers in the memo } for I := 0 to InMemo.Lines.Count - 1 do List.Add(InMemo.Lines[I]); { Sort the list } List.Sort(); { Copy the list with numbers to the memo } OutMemo.Clear; for I := 0 to List.Count - 1 do OutMemo.Lines.Add(List[I]); { Free resources } List.Free(); end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|