Board index » delphi » How to use TList.Sort(Compare) ?

How to use TList.Sort(Compare) ?

Hello,

As a member function of objects TList, there is a description in on-line
help file as following between ==, but no Example! Could you write a
short piece of Example for me?

I wrote a failed routing as below between --. Can you figure out how to
assign the CompareTop function to Sort()? Thank you in advance.

Chen Xun

=============================================== Delphi 2.0 On-Line Help
Declaration

procedure Sort(Compare: TListSortCompare);
TListSortCompare = function (Item1, Item2: Pointer): Integer;

Description

The Sort method performs a Quick Sort on the list based on the return
results of Compare.  Compare will return < 0 if Item1 is less and Item2,
0 if they are equal and > 0 if Item1 is greater than Item2.
=======================================================================----------------------------------------------------- My failed routing
type
  TMyClass = Class
  private
    RectList: TList;
    procedure SortListByTop;
    function CompareTop(Item1, Item2: Pointer): integer;
  end;

implementation

procedure TMyClass.SortListByTop;
begin
  RectList := GetRectList;
  RectList.Sort(CompareTop); <- ?????????????????
end;

function TMyClass.CompareTop(Item1, Item2: Pointer): Integer;
begin
  result := TRect(Item1).Top - TRect(Item2).Top;
end;
-----------------------------------------------------------------------

 

Re:How to use TList.Sort(Compare) ?


Quote
Chen Xun wrote:

This is what the Sort expects:

Quote
> TListSortCompare = function (Item1, Item2: Pointer): Integer;
> function TMyClass.CompareTop(Item1, Item2: Pointer): Integer;

This is function(Item1, Item2 : Pointer : integer of class;
                                                  ^^^^^^^^
Meaning, it takes an additional parameter. Let the compare function
be a standard standalone function, not a member of any class.

M.

--
Martin Larsson, author of several unknown utilities for DOS and Windows.
mailto:martin.lars...@delfi-data.msmail.telemax.no
http://www.delfidata.no/users/~martin
X.400:G=martin;S=larsson;O=delfi-data;P=msmail;A=telemax;C=no

Re:How to use TList.Sort(Compare) ?


Hello,

As a member function of objects TList, there is a description in on-line
help file as following between ==, but no Example! Could you write a
short piece of Example for me?

I wrote a failed routing as below between --. Can you figure out how to
assign the CompareTop function to Sort()? Thank you in advance.

Chen Xun

=============================================== Delphi 2.0 On-Line Help
Declaration

procedure Sort(Compare: TListSortCompare);
TListSortCompare = function (Item1, Item2: Pointer): Integer;

Description

The Sort method performs a Quick Sort on the list based on the return
results of Compare.  Compare will return < 0 if Item1 is less and Item2,
0 if they are equal and > 0 if Item1 is greater than Item2.
=======================================================================----------------------------------------------------- My failed routing
type
  TMyClass = Class
  private
    RectList: TList;
    procedure SortListByTop;
    function CompareTop(Item1, Item2: Pointer): integer;
  end;

implementation

procedure TMyClass.SortListByTop;
begin
  RectList := GetRectList;
  RectList.Sort(CompareTop); <- ?????????????????
end;

function TMyClass.CompareTop(Item1, Item2: Pointer): Integer;
begin
  result := TRect(Item1).Top - TRect(Item2).Top;
end;
-----------------------------------------------------------------------

Other Threads