RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
Classes.TListSortCompare Type

TListSortCompare is a type of callback function called used internally by the TList class.

Pascal
TListSortCompare = function (Item1, Item2: Pointer): Integer;
C++
(Item1, Item2: Pointer): Integer ( TListSortCompare)();

You define a TListSortCompare function when you need to use the TList Sort method. It is invoked by the internal implementation of the Sort method to sort the TList elements into sequence. This user supplied aspect of the sort process is required since TList is a list of pointers with no inherent sort sequence.  

Item1 and Item2 are 2 elements from the list. When these are passed to the TListSortCompare function, the Sort method is asking which order they should be in. The comparison returns a value determined by the relative values of Item1 and Item2, as shown in this table:  

Value 
Description 
>0 (positive)  
Item1 is greater than Item2  
0  
Item1 is equal to Item2  
<0 (negative)  
Item1 is less than Item2  

 

C++ Examples: 

/*
The following code sorts the objects in a list in
alphabetical order based on their names. It assumes that the
list contains only TMyClass references.  The CompareNames
function performs the comparisons between objects in the list
and is of type TListSortCompare:

typedef int __fastcall (*TListSortCompare)(void * Item1, void * Item2);

The list is sorted when the user clicks a button.
*/

#include <memory>       //for STL auto_ptr class

class TMyClass : public TObject
{
__published:    // IDE-managed Components
private:    // User declarations
public:     // User declarations
  AnsiString MyString;
    __fastcall TMyClass(AnsiString mystr);
};

__fastcall TMyClass::TMyClass(AnsiString mystr)
    : TObject()
{
  MyString = mystr;
}

void __fastcall  DisplayTList(TList *TheList)
{
    // Now paint the items onto the paintbox}
    short Y = 10;             // Variable used in TextOut function
    for (byte B = 0; B < TheList->Count; B++)
    {
      TMyClass *temp = reinterpret_cast<TMyClass *>(TheList->Items[B]);
      Form1->Canvas->TextOut(10, Y, temp->MyString);
      Y = Y + 30;  // Increment Y Value again
    }
}

int __fastcall CompareNames(void *Item1, void *Item2)
{
  return CompareText((reinterpret_cast<TMyClass *>(Item1))->MyString,
    (reinterpret_cast<TMyClass *>(Item2))->MyString);
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  MyList->Sort(CompareNames);
  Refresh();
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  static std::auto_ptr<TList> _MyListCleaner(MyList = new TList()); // Create a list
  for (int I = 0; I < ComponentCount; I++)
  {
    TMyClass *MyObject = new TMyClass(Components[I]->Name);  // create a class instance
    MyList->Add(MyObject);      // add instance to list
  }
}

void __fastcall TForm1::FormPaint(TObject *Sender)
{
  DisplayTList(MyList);
}

 

Delphi Examples: 

{
The following code sorts the objects in a list in
alphabetical order based on their names. It assumes that the
list contains only TMyClass references.  The CompareNames
function performs the comparisons between objects in the list
and is of type TListSortCompare.  The list is sorted when the
user clicks a button.
}
type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  TMyClass = class
    MyString: string;
    constructor Create(S: string);
  end;

var
  Form1: TForm1;
  MyList: TList;

implementation

{$R *.dfm}

constructor TMyClass.Create(S: string);
begin
  inherited Create;
  MyString := S;
end;

procedure DisplayTList(TheList: TList);
var
  B: Byte;
  Y: Word;
  str: string;
  Temp: TMyClass;
begin
    { Now paint the items onto the paintbox}
    Y := 10;             {Variable used in TextOut function}
    for B := 0 to (TheList.Count - 1) do
    begin
      Temp:= TheList.Items[B];
      if (Temp is TMyClass) then
      begin
        str:= Temp.MyString;   // clear string
        Form1.Canvas.TextOut(10, Y, str);
        Y := Y + 30;  {Increment Y Value again}
      end;
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
  MyObject: TMyClass;
begin
  MyList := TList.Create;              {Create a list }
  for I := 0 to ComponentCount - 1 do
    begin
      MyObject := TMyClass.Create(Components[I].Name);  { create a class instance }
      MyList.Add(MyObject);      { add instance to list }
    end;
end;

function CompareNames(Item1, Item2: Pointer): Integer;
begin
  Result := CompareText(TMyClass(Item1).MyString, TMyClass(Item2).MyString);
//  MessageDlg('Compare ' + TMyClass(Item1).MyString + ' to ' + TMyClass(Item2).MyString,
//                 mtInformation, [mbOk], 0);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  MyList.Sort(@CompareNames);
  Refresh;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  B: Byte;
  Temp: TMyClass;
begin
    for B := 0 to (MyList.Count - 1) do
    begin
      if (Temp is TMyClass) then
      begin
        Temp:= MyList.Items[B];
        Temp.Free;
      end;
    end;
    MyList.Free;                         {Free memory for list}
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  DisplayTList(MyList);
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!