Enable Sort() with context in TFPList and TList
Original Reporter info from Mantis: kluug.net @onpok
-
Reporter name: Ondrej Pokorny
Original Reporter info from Mantis: kluug.net @onpok
- Reporter name: Ondrej Pokorny
Description:
Unit sortbase supports sorting with a custom "Context" parameter. Yet it is not enabled in TFPList and TList.
The attached patch adds Sort() overloads with this Context parameter to TFPList and TList.
Steps to reproduce:
program Project1;
{$mode delphi}
uses Contnrs, Math;
type
TItem = class
public
Value: Integer;
constructor Create(aValue: Integer);
end;
TSortParameter = class
public
Desc: Boolean;
end;
{ TItem }
constructor TItem.Create(aValue: Integer);
begin
inherited Create;
Value := aValue;
end;
procedure WritelnItems(L: TObjectList);
var
I: TItem;
begin
for I in L do
Writeln(I.Value);
end;
function Compare(Item1, Item2, Context: Pointer): Integer;
var
xItem1: TItem absolute Item1;
xItem2: TItem absolute Item2;
xParam: TSortParameter absolute Context;
begin
Result := CompareValue(xItem1.Value, xItem2.Value);
if xParam.Desc then
Result := -Result;
end;
var
L: TObjectList;
I: Integer;
B: Boolean;
P: TSortParameter;
const
AscDesc: array[Boolean] of string = ('ASC', 'DESC');
begin
L := TObjectList.Create;
for I := 1 to 5 do
L.Add(TItem.Create(I));
P := TSortParameter.Create;
for B in Boolean do
begin
Writeln(AscDesc[B]);
P.Desc := B;
L.Sort(Compare, P);
WritelnItems(L);
Writeln;
end;
P.Free;
L.Free;
ReadLn;
end.
Mantis conversion info:
- Mantis ID: 35962
- Version: 3.3.1
- Fixed in version: 3.3.1
- Fixed in revision: 42798 (#91ba1214)
- Target version: 3.2.0