unit MainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Generics.Collections;
type
TSortObjectList<T: class> = class(TObjectList<T>)
type TCompareItems<T> = function(AItem, BItem: T): Integer of Object;
procedure QuickSort(Left, Right: Integer; ACompareMethod: TCompareItems<T>); overload;
end;
TMyItem = class(TObject)
Dummy: String;
end;
TTestList = class(TSortObjectList<TMyItem>)
end;
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo; { enthält sieben Zeilen mit jeweils einem Buchstaben }
procedure Button1Click(Sender: TObject);
private
function CompareTest(AItem, BItem: TMyItem): Integer;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TSortObjectList<T>.QuickSort(Left, Right: Integer; ACompareMethod: TCompareItems<T>);
var I, J: Integer;
Pivot, Temp: T;
begin { QuickSort procedure from Classes.pas. }
repeat
I := Left;
J := Right;
Pivot := Items[(Left + Right) shr 1];
repeat
while ACompareMethod(Items[I], Pivot) < 0 do Inc(I);
while ACompareMethod(Items[J], Pivot) > 0 do
Dec(J);
if I <= J then begin
Temp := Items[I];
Items[I] := Items[J];
Items[J] := Temp;
Inc(I);
Dec(J);
end;
until I > J;
if Left < J then QuickSort(Left, J, ACompareMethod);
Left := I;
until I >= Right;
end;
procedure TForm1.Button1Click(Sender: TObject);
var A : TTestList;
j : Integer;
function MakeTestItem(AString: String): TMyItem;
begin Result := TMyItem.Create; Result.Dummy := AString; end;
begin
A := TTestList.Create(True);
for j := 0 to Pred(Memo1.Lines.Count) do A.Add(MakeTestItem(Memo1.Lines[j]));
A.QuickSort(0, Pred(A.Count), CompareTest);
Memo1.Clear;
for j := 0 to Pred(A.Count) do Memo1.Lines.Add(A[j].Dummy);
A.Free;
end;
function TForm1.CompareTest(AItem, BItem: TMyItem): Integer;
begin
Result := CompareStr(AItem.Dummy, BItem.Dummy);
end;
end.