Re:TList.Sort?? How to use
In article <37D33096.36A9F...@pcisys.net>, Gregory Elder <gel...@pcisys.net>
writes:
Quote
>Can someone explain how to use the sort method for a TList? I have
>defined a compare function as listed in the on-line help. I.E, the
>header for it is like this:
>function CompareID(Item1, Item2 : Pointer) : integer;
>begin
> { code for compare is here }
>end;
>I have a variable named ScoreList which is a TList object. However,
>when I try to do
>ScoreList.Sort(CompareID)
>I get an error; "Incompatible types: regular procedure and method
>pointer"
Is your procedure header and definition really "function CompareID(Item1, Item2
: Pointer) : integer;" and not "function TForm1.CompareID(Item1, Item2 :
Pointer) : integer;".with a header in the TForm1 type definition.
The former is a regular procedure, the latter is a method, Otherwise it looks
OK. Following is a s{*word*99} of code which defines two sorting procedures (sorting
on different strings in a translation record) and sorts on one or the other
depending on a setting in MyLanguage :-
interface
type
TForm1 = class(TForm)
EngToGerBtn: TButton;
<snip>
procedure TransTypeBtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function EngSort(Ptr1, Ptr2 : pointer) : integer;
function GerSort(Ptr1, Ptr2 : pointer) : integer;
var
Form1: TForm1;
Dictionary : TList;
implementation
<snip>
function EngSort(Ptr1, Ptr2 : pointer) : integer;
begin
Result := AnsiCompareText(TTranslate(Ptr1^).English,
TTranslate(Ptr2^).English);
end;
function GerSort(Ptr1, Ptr2 : pointer) : integer;
begin
Result := AnsiCompareText(TTranslate(Ptr1^).German,
TTranslate(Ptr2^).German);
end;
procedure Tform1.FillKnownListBox;
{fills a list box with the words you know}
var
i : integer;
begin
{sort the dictionary as appropriate}
case MyLanguage of
ltEnglish : Dictionary.Sort(EngSort);
ltGerman : Dictionary.Sort(GerSort);
end;
<snip>
end;
Alan Lloyd
alangll...@aol.com