Solved

Sorting TList's

Posted on 2001-07-30
5
375 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now