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.
Your compare function needs to return 0 for equality. Like this:
It's easier to use
TComparer<POrder>.Construct
to make a comparer.Or use the default date time comparer:
I've not compiled any of this so I expect there will be minor errors.