"Stack overflow" error with Generic TList.Sort

423 Views Asked by At

I've implemented Generics.Defaults.TComparer for sorting a typed TList. However when calling the Sort method, it throws an exception "Stack overflow at..."

Why is the Stack overflow error occurring?

// Declarations

  TOrder = record
    ID: Integer;
    OrderDate: TDate;
  end;

  POrder = ^TOrder;

  FOrderList: TList<POrder>;

  TComparer_OrderDate = class(TComparer<POrder>)
  public
    function Compare(const a, b: POrder): Integer; override;
  end;

function TComparer_OrderDate.Compare(const a, b: POrder): Integer;
begin
  Result := 0;

  if (a^.OrderDate> b^.OrderDate) then
    Result := 1
  else
    Result := -1;
end;


  // This code inside a button OnClick event: 
  FOrderList := TList<POrder>.Create;

  FComparer_OrderDate := TComparer_OrderDate.Create;

  FOrderList.Sort(FComparer_OrderDate); // <--- 'stack overflow' error.
1

There are 1 best solutions below

0
On

Your compare function needs to return 0 for equality. Like this:

function TComparer_OrderDate.Compare(const a, b: POrder): Integer;
begin  
  if (a^.OrderDate > b^.OrderDate) then
    Result := 1
  else if (a^.OrderDate < b^.OrderDate) then
    Result := -1
  else
    Result := 0;
  end;
end;

It's easier to use TComparer<POrder>.Construct to make a comparer.

FOrderList.Sort(
  TComparer<POrder>.Construct(
    function(const a, b: POrder): Integer
    begin  
      if (a^.OrderDate > b^.OrderDate) then
        Result := 1
      else if (a^.OrderDate < b^.OrderDate) then
        Result := -1
      else
        Result := 0;
      end;
    end
  )
);

Or use the default date time comparer:

DateTimeComparer := TComparer<TDateTime>.Default;
FOrderList.Sort(
  TComparer<POrder>.Construct(
    function(const a, b: POrder): Integer
    begin  
      Result := DateTimeComparer.Compare(a^.OrderDate, b^.OrderDate);
    end
  )
);

I've not compiled any of this so I expect there will be minor errors.