Board index » delphi » TList.Sort - How to write a comparison function

TList.Sort - How to write a comparison function

Hello everybody

How do I declare a comparison function for an TList with the following
structure:

type

  PStringItem = ^TStringItem;
  TStringItem = record
                  aString: string;
                  aPointer: Pointer;
                  anObject: Pointer;
                end;

var
 SList:TList;
 PSI: PStringItem;

SList is filled in the following way

for Counter:=0 to 99 do
begin
  New(PSI);
  SList.Add(PSI);
end;

I Want to sort SList based on the values in PSI^.aString. How do I
accomplish
this? The structure of aFunction in the command SList.Sort(aFunction) is not
clear to me.

Jos

 

Re:TList.Sort - How to write a comparison function


On Wed, 11 Feb 1998 12:04:55 +0100, "Odysseus" <Jos.Helm...@pi.net>
wrote:

Quote
>How do I declare a comparison function for an TList with the following
>structure:

>type
>  PStringItem = ^TStringItem;
>  TStringItem = record
>                  aString: string;
>                  aPointer: Pointer;
>                  anObject: Pointer;
>                end;

function PSICompare(Item1, Item2: Pointer): Integer;
begin
  if (PStringItem(Item1)^.aString < PStringItem(Item2)^.aString) then
    Result := -1 else
  if (PStringItem(Item1)^.aString > PStringItem(Item2)^.aString) then
    Result := 1 else
    Result := 0;
end;

Quote
>for Counter:=0 to 99 do
>begin
>  New(PSI);
>  SList.Add(PSI);
>end;

SList.Sort(PSICompare);

---
Yorai Aminov
El-On Software Systems
http://ourworld.compuserve.com/homepages/yaminov

Re:TList.Sort - How to write a comparison function


On Wed, 11 Feb 1998 11:24:37 GMT, yami...@trendline.co.il (Yorai

Quote
Aminov) wrote:
> function PSICompare

Or:

function PSICompare(Item1, Item2: Pointer): Integer;
begin
  Result := AnsiCompareStr( (PStringItem(Item1)^.aString,
     PStringItem(Item2)^.aString))
end;

--
Rick Rogers (TeamB) | Fenestra Technologies

Other Threads