[Lazarus] Example requested for TList and Sort using data type of Object (or record).

Mattias Gaertner nc-gaertnma at netcologne.de
Wed May 11 08:51:04 CEST 2011


On Wed, 11 May 2011 15:02:23 +1000
Peter Williams <peterericwilliams at gmail.com> wrote:

> Hi all,
> 
> I have been googling without success looking for Lazarus TList
> examples using the Sort procedure. I want to use a TObject or Record
> data type.
> 
> Does anyone here know of any examples of this (TList, Sort and Object
> or Record data types) ? I realize that I need to write my own compare
> procedure and I don't have a problem with this (in theory :-).
> 
> I would appreciate a link to some pasted code in http://pastebin.com
> -- if you can supply it. Or a url to a webpage about this.
> 
> nb - I have no preference for using or not using Generic or Specialize
> keywords. I just want something that works.

function SortComponentsForName(Item1, Item2: Pointer): integer;
var
  Comp1: TComponent absolute Item1;
  Comp2: TComponent absolute Item2;
begin
  Result:=CompareText(Comp1.Name,Comp2.Name);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  l: TList;
begin
  l:=TList.Create;
  l.Add(Button2);
  l.Add(Button1);
  l.Sort(@SortComponentsForName);
end;

Mattias




More information about the Lazarus mailing list