unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TData =
class
Datum: TDateTime;
DatumStr:
String;
end;
type
TForm1 =
class(TForm)
ListBox1: TListBox;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
procedure RandomizeArrays;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
BorderIcons := [biSystemmenu];
RandomizeArrays;
end;
//-----------------------------------------------------------------------
procedure TForm1.RandomizeArrays;
var i:integer;
begin
randomize;
Listbox1.Clear;
for i:= 0
to (400-1)
do
begin
Listbox1.items[i]:= DatetoStr(Random(42000));
end;
Label1.Caption:= inttostr(i)+'
'+'
Zufallsdatums erzeugt!';
end;
function CompareDates(List: TStringList; Index1, Index2: Integer): Integer;
var
d1, d2: TDateTime;
begin
d1 := StrToDate(List[Index1]);
d2 := StrToDate(List[Index2]);
if d1 < d2
then
Result := -1
else if d1 > d2
then Result := 1
else
Result := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
//sl: TStringList;
List: TStringList;
begin
{ sl := TStringList.Create;
try
// listbox1.Sorted := False !
sl.Assign(listbox1.Items);
sl.CustomSort(CompareDates); //<- ERROR
listbox1.Items.Assign(sl);
finally
sl.Free
end; }
List:=TStringlist.Create;
try
ListBox1.Items.BeginUpdate;
try
List.Assign(ListBox1.Items);
List.CustomSort(@CompareDates);
//<-Error
ListBox1.Items.Assign(List);
finally
ListBox1.Items.EndUpdate;
end;
finally
List.Free;
end;
end;
end.