1. Home
  2. Computing & Technology
  3. Delphi Programming

Sort a Generic List using Anonymous Comparer Method
How to Sort a Generic TList<T> using an Anonymous Method

By , About.com Guide

Delphi 2009 adds Generics and Anonymous methods to the Delphi language.

When you have objects in some list - one of a commonly required tasks is to sort the list: either ascending, descending or using some list-specific algorithm.

Using anonymous methods you can apply the sorting function directly "inline" with the call to the Sort method.

TList<integer>.Sort(IComparer)

Suppose you have a list of integer numbers. Your task is to sort the numbers in the list.

Here's the code:

uses generics.defaults, generics.collections,...

...

type
  TIntegerComparer = TComparer<integer>;

...

var
  lg : TList<integer>;
  n : integer;
begin
  lg := TList<integer>.Create;
  try
    lg.Add(2) ;
    lg.Add(4) ;
    lg.Add(3) ;
    lg.Add(4) ;
    lg.Add(5) ;
    lg.Add(1) ;

    //will display 2,4,3,4,5,1
    for n in lg do ShowMessage(IntToStr(i)) ;

    //default ascending comparer
    lg.Sort(TComparer<integer>.Default) ;

    //will display 1,2,3,4,4,5
    for n in lg do ShowMessage(IntToStr(i)) ;

    //descending using an anonymous method
    lg.Sort(TIntegerComparer.Construct(
      function (const L, R: integer): integer
      begin
        result := R - L;
      end
      )) ;

    //will display 5,4,4,3,2,1
    for n in lg do ShowMessage(IntToStr(i)) ;

  finally
    lg.Free;
  end;
end;
Note: the TList<T>.Sort methods expects an IComparer<T> object. IComparer<T> is an interface defined in the generics.defaults unit.

TComparer, also defined in the Generics.Defaults, is an abstract base class for IComparer<T> implementations.

The Construct method of the TComparer<T> is a class method defined as:

class function Construct(const Comparison: TComparison<T>): IComparer<T>;
The above returns an IComparer<T> we need for the Sort method AND accepts an anonymous method:
TComparison<T> = reference to function(const Left, Right: T): Integer;

Finally, you have a Sort method with an anonymous method as "parameter":

lg.Sort(TIntegerComparer.Construct(
  function (const L, R: integer): integer
  begin
    result := R - L;
  end
  )) ;

For a non-generic TList object: How to sort items in a TList object.

Delphi tips navigator:
» Override Global Screen.Cursor Change for a Cancel type Button is Delphi Applications
« Prevent a Delphi Form from Being Moved Off Screen

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2009 Tips
  7. Sort a Generic List using Anonymous Comparer Delphi Method

©2009 About.com, a part of The New York Times Company.

All rights reserved.