Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

Sorting TList's

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
Kristian
Asked:
Kristian
  • 2
  • 2
1 Solution
 
DraregCommented:

You can use TList.Sort
0
 
KristianAuthor Commented:
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
 
PudderCommented:
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
 
PudderCommented:
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
 
KristianAuthor Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now