Sorting records using TList.Sort

Hi
I am a trial and error man. Right now I try to figure out how to sort records.
I have some questions and some coding.

The sort routine Ive found on the net and in the helpfile on Delphi 5 looks like this:

function Compare(Item1, Item2: Pointer): Integer;
begin
if PM(Item1)^.TestPM(Item2)^.Test then Result :=1;
if PM(Item1)^.Test=PM(Item2)^.Test then Result :=0;
end;
Above PM is what I used when I declared the type.
Type
PM = ^TM;
TM = record
Test: String;
Why do I have to use PM and and not PTette (Var PTette : PM)? Is it because I use it in a function?

Does the routine above sort more than 2 items without me telling it to?
Because the only call Ive found to the routine is: List.Sort(@Compare);
I tried without the '@', dont even know what that character does to it.

In my program I try to sort some numbers (3 to 0), Is it in that coding I make the error?

procedure TForm1.FormCreate(Sender: TObject);
var
a: Integer;
begin

List:=TList.Create;
getmem(PTette, 4);
for a:=0 to 3 do
Begin
Tette.Test:=Inttostr(3-a);
List.Add(PTette);
end;
end;

Or maybe the big error lies in this coding?
I am unsure if I am on the right way using TList.Items to point to the record I want to use, if not, what is the right way?
Have I got it all right when I understand the ^like this?
^P points to an address in the memory, but p^ is used to use the contense of an address?


procedure TForm1.Button1Click(Sender: TObject);

Begin;

begin
List.Items[0];
Edit1.text:=PTette^.Test;
List.Items[1];
Edit2.Text:=Ptette^.Test;
List.Items[2];
Edit3.Text:=Ptette^.Test;
List.Items[3];
Edit4.Text:=Ptette^.Test;
end;

I hope someone understands my questions, otherwise tell me so I can make another try to ask my questions.

Regards Stefan, Sweden



Comments

  • : Hi
    : I am a trial and error man. Right now I try to figure out how to sort records.
    : I have some questions and some coding.
    :
    : The sort routine Ive found on the net and in the helpfile on Delphi 5 looks like this:
    :
    : function Compare(Item1, Item2: Pointer): Integer;
    : begin
    : if PM(Item1)^.TestPM(Item2)^.Test then Result :=1;
    : if PM(Item1)^.Test=PM(Item2)^.Test then Result :=0;
    : end;
    : Above PM is what I used when I declared the type.
    : Type
    : PM = ^TM;
    : TM = record
    : Test: String;
    : Why do I have to use PM and and not PTette (Var PTette : PM)? Is it because I use it in a function?
    :
    : Does the routine above sort more than 2 items without me telling it to?
    : Because the only call Ive found to the routine is: List.Sort(@Compare);
    : I tried without the '@', dont even know what that character does to it.
    :
    : In my program I try to sort some numbers (3 to 0), Is it in that coding I make the error?
    :
    : procedure TForm1.FormCreate(Sender: TObject);
    : var
    : a: Integer;
    : begin
    :
    : List:=TList.Create;
    : getmem(PTette, 4);
    : for a:=0 to 3 do
    : Begin
    : Tette.Test:=Inttostr(3-a);
    : List.Add(PTette);
    : end;
    : end;
    :
    : Or maybe the big error lies in this coding?
    : I am unsure if I am on the right way using TList.Items to point to the record I want to use, if not, what is the right way?
    : Have I got it all right when I understand the ^like this?
    : ^P points to an address in the memory, but p^ is used to use the contense of an address?
    :
    :
    : procedure TForm1.Button1Click(Sender: TObject);
    :
    : Begin;
    :
    : begin
    : List.Items[0];
    : Edit1.text:=PTette^.Test;
    : List.Items[1];
    : Edit2.Text:=Ptette^.Test;
    : List.Items[2];
    : Edit3.Text:=Ptette^.Test;
    : List.Items[3];
    : Edit4.Text:=Ptette^.Test;
    : end;
    :
    : I hope someone understands my questions, otherwise tell me so I can make another try to ask my questions.
    :
    : Regards Stefan, Sweden
    :
    :
    :
    :
    As far as I can tell, you have many issues with pointers. First let me start with the Sort() method. Sort() itself is coded as a quicksort algorithm, which takes the Compare function to compare two pointers with eachother. It sorts the entire list and the function you provide gives it the rules to sort by.
    The @ character takes the address of the identifier following it can create a pointer to that address (see the help file and the Addr() function for more info).
    The code to fill your list contained several errors. Here is an updated one.
    [code]
    : procedure TForm1.FormCreate(Sender: TObject);
    : var
    : a: Integer;
    [b]PTette: PM;[/b]
    : begin
    :
    : List:=TList.Create;
    : getmem(PTette, 4);
    : for a:=0 to 3 do
    : Begin
    : [b]P[/b]Tette[b]^[/b].Test:=Inttostr(3-a);
    : List.Add(PTette);
    : end;
    : end;
    [/code]
    The ^-symbol is used to dereference the pointer. See the "What does ^ mean?"-thread on this board for more info.
    You button click code also contained some errors. Here is the corrected one:
    [code]
    : procedure TForm1.Button1Click(Sender: TObject);
    :
    : Begin;
    :
    :[b]//[/b] begin <= is not necessary
    : [b]PTette := PM([/b]List.Items[0][b])[/b]; // copy the pointer to the first address into the variable
    : Edit1.text:=PTette^.Test; // get the value
    : Edit2.Text:=[b]PM(List.Items[1])[/b]^.Test; // directly read the record from the list
    : Edit3.Text:=PM(List.Items[2])^.Test;
    : Edit4.Text:=PM(List.Items[3])^.Test;
    : end;
    [/code]

    I hope my answers were clear enough. For more info on the various methods and functions I urge you te read the help files, because that and the demos programs are the best information source available. I learned Delphi from them.
  • : : Hi
    : : I am a trial and error man. Right now I try to figure out how to sort records.
    : : I have some questions and some coding.
    : :
    : : The sort routine Ive found on the net and in the helpfile on Delphi 5 looks like this:
    : :
    : : function Compare(Item1, Item2: Pointer): Integer;
    : : begin
    : : if PM(Item1)^.TestPM(Item2)^.Test then Result :=1;
    : : if PM(Item1)^.Test=PM(Item2)^.Test then Result :=0;
    : : end;
    : : Above PM is what I used when I declared the type.
    : : Type
    : : PM = ^TM;
    : : TM = record
    : : Test: String;
    : : Why do I have to use PM and and not PTette (Var PTette : PM)? Is it because I use it in a function?
    : :
    : : Does the routine above sort more than 2 items without me telling it to?
    : : Because the only call Ive found to the routine is: List.Sort(@Compare);
    : : I tried without the '@', dont even know what that character does to it.
    : :
    : : In my program I try to sort some numbers (3 to 0), Is it in that coding I make the error?
    : :
    : : procedure TForm1.FormCreate(Sender: TObject);
    : : var
    : : a: Integer;
    : : begin
    : :
    : : List:=TList.Create;
    : : getmem(PTette, 4);
    : : for a:=0 to 3 do
    : : Begin
    : : Tette.Test:=Inttostr(3-a);
    : : List.Add(PTette);
    : : end;
    : : end;
    : :
    : : Or maybe the big error lies in this coding?
    : : I am unsure if I am on the right way using TList.Items to point to the record I want to use, if not, what is the right way?
    : : Have I got it all right when I understand the ^like this?
    : : ^P points to an address in the memory, but p^ is used to use the contense of an address?
    : :
    : :
    : : procedure TForm1.Button1Click(Sender: TObject);
    : :
    : : Begin;
    : :
    : : begin
    : : List.Items[0];
    : : Edit1.text:=PTette^.Test;
    : : List.Items[1];
    : : Edit2.Text:=Ptette^.Test;
    : : List.Items[2];
    : : Edit3.Text:=Ptette^.Test;
    : : List.Items[3];
    : : Edit4.Text:=Ptette^.Test;
    : : end;
    : :
    : : I hope someone understands my questions, otherwise tell me so I can make another try to ask my questions.
    : :
    : : Regards Stefan, Sweden
    : :
    : :
    : :
    : :
    : As far as I can tell, you have many issues with pointers. First let me start with the Sort() method. Sort() itself is coded as a quicksort algorithm, which takes the Compare function to compare two pointers with eachother. It sorts the entire list and the function you provide gives it the rules to sort by.
    : The @ character takes the address of the identifier following it can create a pointer to that address (see the help file and the Addr() function for more info).
    : The code to fill your list contained several errors. Here is an updated one.
    : [code]
    : : procedure TForm1.FormCreate(Sender: TObject);
    : : var
    : : a: Integer;
    : [b]PTette: PM;[/b]
    : : begin
    : :
    : : List:=TList.Create;
    : : getmem(PTette, 4);
    : : for a:=0 to 3 do
    : : Begin
    : : [b]P[/b]Tette[b]^[/b].Test:=Inttostr(3-a);
    : : List.Add(PTette);
    : : end;
    : : end;
    : [/code]
    : The ^-symbol is used to dereference the pointer. See the "What does ^ mean?"-thread on this board for more info.
    : You button click code also contained some errors. Here is the corrected one:
    : [code]
    : : procedure TForm1.Button1Click(Sender: TObject);
    : :
    : : Begin;
    : :
    : :[b]//[/b] begin <= is not necessary
    : : [b]PTette := PM([/b]List.Items[0][b])[/b]; // copy the pointer to the first address into the variable
    : : Edit1.text:=PTette^.Test; // get the value
    : : Edit2.Text:=[b]PM(List.Items[1])[/b]^.Test; // directly read the record from the list
    : : Edit3.Text:=PM(List.Items[2])^.Test;
    : : Edit4.Text:=PM(List.Items[3])^.Test;
    : : end;
    : [/code]
    :
    : I hope my answers were clear enough. For more info on the various methods and functions I urge you te read the help files, because that and the demos programs are the best information source available. I learned Delphi from them.
    :

    Thank you very much for your help. Im sure I will come back to this forum with more questions.
    I just want to tell that I found whats was missing (besides the things you told me was wrong).

    In the routine where I give the records a value I forgot one important thing.
    It now looks like this:

    procedure TForm1.FormCreate(Sender: TObject);
    var
    a: Integer;
    begin

    List:=TList.Create;
    getmem(PTette, 4);
    for a:=0 to 3 do
    Begin
    new(PTette);
    //PTette^.Test:='3';
    PTette^.Test:=Inttostr(1-a);
    List.Add(PTette);
    end;

    Now it all works. So now I can get back to my real project. Thank you very much for your help.

    egards Stefan
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories