Solved

Sorting TList's

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 127
how can i search if string exist in array ? 3 63
DBGrid or StringGrid ? 6 103
How to save the image in the .cds File ClientDataSet? 1 22
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
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…

808 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