Board index » delphi » Anybody have a code snippet using Sort on a TList?

Anybody have a code snippet using Sort on a TList?

I'm having a bad crash problem as soon as my program references the
compare function when trying to use the sort method on an item of type
TList.  I've tried eveything I can think of, and am actually beginning
to wonder if the method works.

Sigh .... I'm sure its me and not the method.  If anyone has a code
snippet exemplifying the use of the sort method and the compare
function I would certainly appreciate it.

Thanks much,
Rich

----------------------------------------------------------------------
Richard V. Downing              Work email: richard.down...@vsin.com
Senior Software Engineer        Home email: r...@mindspring.com
VideoConferencing Systems, Inc. Web: http://www.mindspring.com/~rvd
Norcross, Ga
------------ When surrounded by wolves 'tis best to howl -------------

 

Re:Anybody have a code snippet using Sort on a TList?


Quote
Rich Downing wrote:

> I'm having a bad crash problem as soon as my program references the
> compare function when trying to use the sort method on an item of type
> TList.  I've tried eveything I can think of, and am actually beginning
> to wonder if the method works.

> Sigh .... I'm sure its me and not the method.  If anyone has a code
> snippet exemplifying the use of the sort method and the compare
> function I would certainly appreciate it.

> Thanks much,
> Rich

> ----------------------------------------------------------------------
> Richard V. Downing              Work email: richard.down...@vsin.com
> Senior Software Engineer        Home email: r...@mindspring.com
> VideoConferencing Systems, Inc. Web: http://www.mindspring.com/~rvd
> Norcross, Ga
> ------------ When surrounded by wolves 'tis best to howl -------------

Try this:

function SortIt(Item1, Item2: Pointer): Integer;
begin
  if PInteger(Item1)^ < PInteger(Item2)^ then Result := -1 else
  if PInteger(Item1)^ > PInteger(Item2)^ then Result := 1 else
   Result := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Int1 : Integer;
  Int2 : Integer;
  Int3 : Integer;
  l : TList;
begin
  l :=TList.Create;
  Int1 := 3;
  Int2 := 2;
  Int3 := 1;
  l.Add(@Int1);
  l.Add(@Int2);
  l.Add(@Int3);
  l.Sort(SortIt);
  Memo1.Lines.Add(IntToStr(PInteger(l.Items[0])^));
  Memo1.Lines.Add(IntToStr(PInteger(l.Items[1])^));
  Memo1.Lines.Add(IntToStr(PInteger(l.Items[2])^));
  l.Free;

end;

--
Joe C. Hecht
Borland Delphi Developer Support Group
http://www.borland.com

Re:Anybody have a code snippet using Sort on a TList?


TList doesn't have a sort method or property (TStringList does) - maybe the
Stringlist isn't created.

HTH,

--
Paul Motyer
pa...@pccity.com.au - now working!
Soft Stuff, Croydon, Australia, 3136

Rich Downing <r...@mindspring.com> wrote in article
<5f4q8n$...@camel1.mindspring.com>...

Quote
> I'm having a bad crash problem as soon as my program references the
> compare function when trying to use the sort method on an item of type
> TList.  I've tried eveything I can think of, and am actually beginning
> to wonder if the method works.

> Sigh .... I'm sure its me and not the method.  If anyone has a code
> snippet exemplifying the use of the sort method and the compare
> function I would certainly appreciate it.

> Thanks much,
> Rich

> ----------------------------------------------------------------------
> Richard V. Downing              Work email: richard.down...@vsin.com
> Senior Software Engineer        Home email: r...@mindspring.com
> VideoConferencing Systems, Inc. Web: http://www.mindspring.com/~rvd
> Norcross, Ga
> ------------ When surrounded by wolves 'tis best to howl -------------

Re:Anybody have a code snippet using Sort on a TList?


Quote
>TList doesn't have a sort method or property (TStringList does) - maybe the
>Stringlist isn't created.
>HTH,
>--
>Paul Motyer
>pa...@pccity.com.au - now working!
>Soft Stuff, Croydon, Australia, 3136

TList does have a sort method.  Perhaps it was added in Version 2. I
finally got around my problem (my stupidity) and the sort method works
fine.  If you have version 2 check out the TList methods.

Regards,
Rich

----------------------------------------------------------------------
Richard V. Downing              Work email: richard.down...@vsin.com
Senior Software Engineer        Home email: r...@mindspring.com
VideoConferencing Systems, Inc. Web: http://www.mindspring.com/~rvd
Norcross, Ga
------------ When surrounded by wolves 'tis best to howl -------------

Other Threads