Comparing Methods using Generics

This is my first post of 2011. I will hopefully be doing more posts this year. These will include my work, tutorials, snippets of code and what I find on the web. Hopefully this year will be a good one, with new projects, more ideas and more motivation.
In Delphi 2009 appeared the Generics (containers-of-type-T) in the Delphi programming language. I have been playing with them in Delphi 2010 and here you can see my results. The new library is quite easy to use and with the help of this libraries we can enhance our code. We can also use the Anonymous methods and call the function "inline" in the sort method. 
Using the Generics.defaults and Generics.Collections we can build new container or comparer classes and pass to them the type-T (Integer, String, TObject, etc.).
  • Default comparer:
This example will create an Integer comparer and it will use a TIntegerList to sort its values:

unit uComparing;

interface

uses
    generics.defaults, generics.collections;

type
    TIntegerComparer = class(TComparer<Integer>);
    TIntegerList = class(TList<Integer>);

implementation

end. 
usage:
procedure TForm1.Button1Click(Sender: TObject);
var
    integerList : TIntegerList;
    i: Integer;
begin
    integerList := TIntegerList.Create(TIntegerComparer.default);
    for i := 0 to memo1.Lines.count - 1 do
        integerList.Add(StrToInt(Memo1.Lines[i]));
    integerList.Sort;
    for i := 0 to integerList.count - 1 do
        Memo2.Lines.Add(IntToStr(integerList[i]));
end;
  • Custom Comparer:
The custom comparer will use a comparer method to sort the values in a descendant way.

unit uComparing;

interface

uses
    generics.defaults, generics.collections;

type
    TIntegerComparer = class(TComparer<Integer>)
    public
        function Compare(const Left, Right : Integer) : Integer; override;
    end;

    TIntegerList = class(TList<Integer>);


implementation

{ TIntegerComparer }

function TIntegerComparer.Compare(const Left, Right: Integer): Integer;
begin
    result := Right - Left;
end;

end. 
usage:
procedure TForm1.Button1Click(Sender: TObject);
var
    integerList : TIntegerList;
    i: Integer;
begin
    integerList := TIntegerList.Create(TIntegerComparer.create);
    for i := 0 to memo1.Lines.count - 1 do
        integerList.Add(StrToInt(Memo1.Lines[i]));
    integerList.Sort;
    for i := 0 to integerList.count - 1 do
        Memo2.Lines.Add(IntToStr(integerList[i]));
end;
  • Anonymous Comparer:
The Anonymous comparer is created inside the sort method:

procedure TForm1.Button1Click(Sender: TObject);
var
    integerList : TIntegerList;
    i: Integer;
begin
    integerList := TIntegerList.Create();
    for i := 0 to memo1.Lines.count - 1 do
        integerList.Add(StrToInt(Memo1.Lines[i]));
    integerList.Sort(TIntegerComparer.Construct(
       function (const Left , Right : integer): integer
       begin
         result := Left - Right;
       end
       ));
    for i := 0 to integerList.count - 1 do
        Memo2.Lines.Add(IntToStr(integerList[i]));
end;

Comments

Popular Posts