How to get the maximum Value in a generic TList<Integer>?

delphi, delphi-xe2

Solution

Here's a fun example with a `MaxValue` implementation on a generic container:

{$APPTYPE CONSOLE}

uses
  System.SysUtils, System.Generics.Defaults, System.Generics.Collections;

type
  TMyList<T> = class(TList<T>)
  public
    function MaxValue: T;
  end;

{ TMyList<T> }

function TMyList<T>.MaxValue: T;
var
  i: Integer;
  Comparer: IComparer<T>;
begin
  if Count=0 then
    raise Exception.Create('Cannot call TMyList<T>.MaxValue on an empty list');
  Comparer := TComparer<T>.Default;
  Result := Self[0];
  for i := 1 to Count-1 do
    if Comparer.Compare(Self[i], Result)>0 then
      Result := Self[i];
end;

var
  IntList: TMyList<Integer>;
  DoubleList: TMyList<Double>;
  StringList: TMyList<string>;

begin
  IntList := TMyList<Integer>.Create;
  IntList.AddRange([10, 5, 12, -49]);
  Writeln(IntList.MaxValue);

  DoubleList := TMyList<Double>.Create;
  DoubleList.AddRange([10.0, 5.0, 12.0, -49.0]);
  Writeln(DoubleList.MaxValue);

  StringList := TMyList<string>.Create;
  StringList.AddRange(['David Heffernan', 'Uwe Raabe', 'Warren P', 'Jens Mühlenhoff']);
  Writeln(StringList.MaxValue);

  Readln;
end.

Because we cannot come up with a generic equivalent to `low(Integer)` I raise an exception when the method is called on an empty list.

The output is:

12
 1.20000000000000E+0001
Warren P

Problem

What is the easiest way to get the maximum value in a `TList<Integer>`? ``` function GetMaximum(AList: TList<Integer>): Integer; begin Assert(AList.Count > 0); Result := ?; end; ``` I read that C# has a `AList.Max`, is there something like that in Delphi?

Original source

Related problems