RAD Studio VCL Reference
|
TComparison defines a generic callback function used to compare two generic values.
TComparison<T> = reference to function(const Left, Right: T): Integer;
reference to function(const Left, Right: T): Integer TComparison;
TComparison represents any routine capable of comparing two values of the same type. For example, CompareStr can be represented as TComparison<AnsiString>.
TComparison is primarily used together with the TDelegatedComparer class.
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!
|