Board index » delphi » TList.Sort(Compare: TListSortCompare) ?

TList.Sort(Compare: TListSortCompare) ?

Please could someone explain how to use the Sort method of TList.
Many thanks

Andy
A...@HHInternational.com

 

Re:TList.Sort(Compare: TListSortCompare) ?


first, you create a function of type TListSortCompare, for example:
(Note: there is no TMyForm at the beginning of the function...
"function TMyForm.SortByRx(Item1, Item2: Pointer): Integer;" would be
incorrect)

(In this example, I attach a record type to the data of the TList. This
is what is being referenced here.)

function SortByRx(Item1, Item2: Pointer): Integer;
begin
  Result := AnsiCompareText(PNodeData(Item1)^.SiteName +
                            PNodeData(Item1)^.PlanName +
                            IntToStr(PNodeData(Item1)^.RxNumber),
                            PNodeData(Item2)^.SiteName +
                            PNodeData(Item2)^.PlanName +
                            IntToStr(PNodeData(Item2)^.RxNumber));
end;

then you call it from the TList.Sort method, ie:
MyList.Sort(SortByRx);

From the TList.Sort help:
Sort performs a QuickSort on the list based on the comparison function
Compare.

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

Description

Call Sort to sort the items in the Items array. Compare is a comparison
function that indicates how the items are to be ordered. Compare returns
< 0 if Item1 is less and Item2, 0 if they are equal and > 0 if Item1 is
greater than Item2.

HTH,
Wayne

Quote
Andy H wrote:

> Please could someone explain how to use the Sort method of TList.
> Many thanks

> Andy
> A...@HHInternational.com

Re:TList.Sort(Compare: TListSortCompare) ?


Many thanks Wayne.

The Delphi help file was pretty useless.

PS. SortByRx function maybe should have be called SortBySiteName.

Re:TList.Sort(Compare: TListSortCompare) ?


Quote
> The Delphi help file was pretty useless

The help file should include an example, but doesn't. However, procedural
types are very fully and clearly documented with lots of examples, and
TList.Sort is just one place among many which uses procedural types.

- Rick

Re:TList.Sort(Compare: TListSortCompare) ?


Quote
>The help file should include an example, but doesn't. However, procedural
>types are very fully and clearly documented with lots of examples, and
>TList.Sort is just one place among many which uses procedural types.

>- Rick

Nevertheless Rick, the help file is VERY unhelpful but thanks for your
comments.
However, on the plus side for Borland, ... this newsgroup more than
compensates.
(Thanks to Borland and all the newsgroup participants).

Andy
A...@HHInternational.com

Re:TList.Sort(Compare: TListSortCompare) ?


Quote
Andy H wrote:

> Many thanks Wayne.

You're welcome.

Quote
> The Delphi help file was pretty useless.

> PS. SortByRx function maybe should have be called SortBySiteName.

Maybe. Except that this is not the only sort routine used, and all of
them sort by SiteName + PlanName - the difference occurs after those. So
I guess I should have called it SortBySitePlanRx...

Re:TList.Sort(Compare: TListSortCompare) ?


Quote
> Please could someone explain how to use the Sort method of TList.

You usually put Records into a TList, for example:

   TTaskObject = record
        NextTime: string [32] ;        
        NextDT: TDateTime ;            
        TaskName: string [64] ;        
        LastDT: TDateTime ;            
   end ;  
   PTTaskObject = ^TTaskObject;  
(PS, avoid ANSI strings in a record unless you want a potential memory leak)

Then you declare the comparison function, comparing an element in the
record:

function CompareNext (Item1, Item2: Pointer): Integer;
// Compare returns < 0 if Item1 is less and Item2, 0 if they are equal
// and > 0 if Item1 is greater than Item2.
// CompareText(const S1, S2: string): Integer;
begin
  result := 1 ;
  if PTTaskObject (Item1).NextDT > PTTaskObject (Item2).NextDT then exit ;
  result := -1 ;
  if PTTaskObject (Item1).NextDT < PTTaskObject (Item2).NextDT then exit ;
  result := 0 ;
end ;

Then once you've create the list, just sort it:

  TaskList.Sort (CompareNext) ;

Angus  

Other Threads