Solved

Sorting TList's

Posted on 2001-07-30
5
377 Views
Last Modified: 2010-04-04
Ok ( i feel a bit stupid here but... )
now ive done sorting in TTreeView's and TListView's but now i need to do one in a TList and ive realised that i never did work that one out...!

can someone give me a simple lesson on TList Sorting ( basically the assigning of the procedure part to the sort event ) as i cannot seem to get it running right..

Cheers Kristian
0
Comment
Question by:Kristian
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:Drareg
ID: 6334940

You can use TList.Sort
0
 
LVL 1

Author Comment

by:Kristian
ID: 6334979
yeah ive got that bit!
but how do you assign the sort procedure to the TListSortCompare ????

...............

I have the sort procedure

function TMyFM.LVCompare(sender :TObject;Item1,Item2: Pointer):integer;
begin
  Result := Whatever;;;
end;
Then I Have The TList

var
  MyList : TList;
Begin
  MyList := TList.create;
  {Create a bunch of pointer records and add them ETC...}  

{Then i want to sort MyList using the compare function...
according to delphi help i pass in the compare event/Func to the Sort procedure !!}
but whether i have the sender on the procedure there or not or if i have a compare event locally and assign the procedure to the event and then pass the event into the procedure it still doesnt like it.... Why ?}
end;


A Code Example will probably be more usefull.

K
0
 

Accepted Solution

by:
Pudder earned 50 total points
ID: 6335143
The problem you are experiencing lies in the fact you are
passing the sort procedure a pointer to a method, not a pointer to a procedure.

you defined your sort function as
function TMyFM.LVCompare(sender :TObject;Item1,Item2: Pointer):integer;

remove the function from your form class, making it just a local function, and it runs fine.  I tested to make sure, and this is the case.  Your error message should have been something along the lines of:
[Error] Unit1.pas(38): Incompatible types: regular procedure and method pointer

This means you are passing a method( class function ) when it expects just a function.

the declaration of TListSortCompare is:
type TListSortCompare = function (Item1, Item2: Pointer): Integer;

for your code to have worked, it would need to be:
type TListSortCompare = function (Item1, Item2: Pointer): Integer of object;

note the 'of object' at the end.  of object means method of a class.

Happy coding.

0
 

Expert Comment

by:Pudder
ID: 6335195
Here's my test code, just in case my explanation was not fully understandable...

Notice my sort function, domysort is not a member of TForm1.

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function domysort(Item1, Item2: Pointer): Integer;
begin
  Result := 0;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  with TList.Create do
  try
    sort( domysort );
  finally
    free;
  end;
end;
0
 
LVL 1

Author Comment

by:Kristian
ID: 6336918
Thats great, thank you.

makes sence now !!
I would have presumed that you would have had to have to sender part, but i did try both, with and without. but i dont really understand why the procedure cannot be in a class and has to be local! i wouldnt have thought that would have made that much difference, i suppose its the reverse to why a button click ( with a sender ) HAS to be in a class !!

Thanks anyway
Kristian
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Performance of SQL statement 37 111
Dev express lookupcombo 3 27
Delphi: barcode reading on android platform 1 30
Twebbrowser add css to the header 3 25
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question