Solved

Sorting TList's

Posted on 2001-07-30
5
382 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month3 days, 15 hours left to enroll

630 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