Board index » delphi » More TStringGrid Questions - How to use TList.Sort?

More TStringGrid Questions - How to use TList.Sort?

Hi there,

Thanks for the help regarding the TStringGrid.DeleteRow method. I used the
method suggested by Scott Roberts and it works like a charm - Thanks.

I am trying to sort a TList object which contains a list of pointer type
records. The TList is used to populate a StringGrid and this works fine, but
I would like to sort the records in the StringGrid and I dont know how.

I have seen the TList.Sort method described in the help files but cannot
figure out how to use it. Can anybody help me? This is the data structure I
am using.

   Type
    TLiqlist = ^LiqType;

    LiqType = packed record
      Desc : String[30];
      Visc : Double;
      Temp : Single;
      Density : Single;
    end;

var
  LiqList : TList;
  LiqListItem : TLiqList;
  LiqRecord : LiqType;

The StringGrid contains a list of Liquid descriptions and other properties
such as vicosity etc.
eg.

Water        1.14        20        1000
Milk            2.5           15        1010

and I want to sort the TList by the description ie. LiqList.Items[i]^.Desc
but I don't know how.

Any help would be appreciated.

thanks

Peter Burrow

 

Re:More TStringGrid Questions - How to use TList.Sort?


Quote
helix <he...@vianet.net.au> wrote:
> I have seen the TList.Sort method described in the help files but cannot
> figure out how to use it. Can anybody help me? This is the data structure I
> am using.
>    Type
>     TLiqlist = ^LiqType;
>     LiqType = packed record
>       Desc : String[30];
>       Visc : Double;
>       Temp : Single;
>       Density : Single;
>     end;
> var
>   LiqList : TList;
>   LiqListItem : TLiqList;
>   LiqRecord : LiqType;

function SortLiqList(item1,item2:pointer):integer;
begin
  if tLiqType(item1)^.desc<tliqtype(item2)^.desc then
    result:=-1
  else if tLiqType(item1)^.desc>tliqtype(item2)^.desc then
    result:=+1
  else
    result:=0;
  end;

LiqList.Sort(SortLiqList);

Bye,
   Andy

--
Andreas Hoerstemeier
  email: a...@scp.de          (work)
         a...@hoerstemeier.de (home)
  www:   http://www.hoerstemeier.com

Re:More TStringGrid Questions - How to use TList.Sort?


Hi Andy,

Thanks very much for the sort function - it works perfectly, although I
don't understand how it works yet.

regards
Peter

Quote
Andreas Hoerstemeier wrote in message <733aj9$...@dilbert.scp.de>...
>function SortLiqList(item1,item2:pointer):integer;
>begin
>  if tLiqType(item1)^.desc<tliqtype(item2)^.desc then
>    result:=-1
>  else if tLiqType(item1)^.desc>tliqtype(item2)^.desc then
>    result:=+1
>  else
>    result:=0;
>  end;

>LiqList.Sort(SortLiqList);

>Bye,
>   Andy

>--
>Andreas Hoerstemeier
>  email: a...@scp.de          (work)
>         a...@hoerstemeier.de (home)
>  www:   http://www.hoerstemeier.com

Other Threads