Board index » delphi » TList.Sort?? How to use

TList.Sort?? How to use

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"

I have tried things like the following, all of which do not work either:

ScoreList.Sort(@CompareID);
ScoreList.Sort(TListSortCompare(CompareID));
ScoreList.Sort(TListSortCompare(@CompareID));

So what's the secret?  Thank you.

Greg

 

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

Re:TList.Sort?? How to use


As far as I remember the procedure that you pass to the sort method must be
a straight function and not a method.

So I suspect that the definition of your compareId routine, considering the
compiler message, looks like

function TMyObject.compareId(.....

instead of a true procedure. In other words the function must not be a
member of a class.

Quote
Gregory Elder <gel...@pcisys.net> wrote in message

news:37D33096.36A9FC0B@pcisys.net...
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"

> I have tried things like the following, all of which do not work either:

> ScoreList.Sort(@CompareID);
> ScoreList.Sort(TListSortCompare(CompareID));
> ScoreList.Sort(TListSortCompare(@CompareID));

> So what's the secret?  Thank you.

> Greg

Other Threads