Board index » delphi » Having problems with sort method in tlist, cant declare compare function

Having problems with sort method in tlist, cant declare compare function

I am having problems using the sort method for a tlist.

The code below won't compile

I get the
"Incompatible types pointer and tptdata"

My parameters for the swap items must be of type tptdata.

Any help would be brilliant,

Thanks
Michael Donovan.

----------------------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs;

type

  tPtData = ^fPtData;
  fPtData = record
    Com, Pos, ILevel: integer;
  end;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }

    PtData: tPtData;
    ControlList: tlist;

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function SortFun(Item1, Item2: tPtData): Integer;
begin

  if (tPtData(Item1)^.pos < tPtData(Item2)^.pos) then Result := 0
  else result := 1;

end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: integer;

begin

  ControlList := tList.Create;

//fill list with 100 random int's

  for i := 0 to 99 do
  begin
    New(PtData);
    PtData^.Com := i;
    PtData^.pos := random(50);
    ControlList.Add(PtData);
  end;

//sort list

  ControlList.Sort(SortFun);

//clean up list

  for i := 0 to ControlList.Count - 1 do
  begin
    PtData := ControlList.Items[i];
    Dispose(PtData);
  end;

  ControlList.CLear;

  ControlList.Free;
end;

end.

--

 ------------------------------------------------
|  Michael Donovan                               |
|  Maynooth, Ireland.                            |
|  Phone: 086 8148480 / 01 6290346               |
|  Web site : http://ireland.iol.ie/~michaeld/   |
|  Email : micha...@iol.ie                       |
 ------------------------------------------------

 

Re:Having problems with sort method in tlist, cant declare compare function


Michael,

I didn't check it, but your definition is wrong.

Change the definition to:

function SortFun(Item1, Item2: Pointer): Integer;

Hope that helps.

David.

Re:Having problems with sort method in tlist, cant declare compare function


Quote
[Michael Donovan wrote:

"I get the "Incompatible types pointer and tptdata"
My parameters for the swap items must be of type tptdata."]

Your sort function has to be of type
TListSortCompare = function (Item1, Item2: Pointer): Integer;

So try
function SortFun(Item1, Item2: Pointer): Integer;

                                                 ^^^^^^^^^^^
begin
  if (tPtData(Item1)^.pos < tPtData(Item2)^.pos) then Result := 0
  else result := 1;
end;

Re:Having problems with sort method in tlist, cant declare compare function


In article <36DC13D0.F0C95...@iol.ie>, Michael Donovan <micha...@iol.ie>
writes:

Quote
>function SortFun(Item1, Item2: tPtData): Integer;
>begin

>  if (tPtData(Item1)^.pos < tPtData(Item2)^.pos) then Result := 0
>  else result := 1;

>end;

The sort function has already been declared by Delphi to have parameters of
pointer - you cannot change that but you can type-cast them as you have done.
Your result is incorrect too.

function SortFun(Item1, Item2: pointer): Integer;
begin
  if (tPtData(Item1)^.pos < tPtData(Item2)^.pos) then
    Result := -1
  else
    if (tPtData(Item1)^.pos > tPtData(Item2)^.pos) then
      Result := 1
    else
      result := 0;
end;

BTW your code would be clearer if you followed some of Delphi's conventions for
type and variable names :-

Generic type (of record, variable, object) -  TMyRecord
Variable - MyRecord
Pointer to TMyRecord - PMyRecord
 . . and my convention for pointer names - PtrMyRecord

 . . or conversely starting from the record name :-

Record name - PtData
Type of record - TPtData
Type of Pointer to a TPtData - PPtData
Name of pointer variable PtrPtData

And for the name of a private record in a component - FPtData.

Alan Lloyd
alangll...@aol.com

Other Threads