unit AUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
PRecordTyp = ^TRecordTyp;
TRecordTyp =
record
I1: integer;
S1:
String;
end;
TForm1 =
class(TForm)
Button1: TButton;
ListBox1: TListBox;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function CompareByI1(
const Item1, Item2: PRecordTyp): integer;
begin
Result := 0;
if Item1^.I1 > Item2^.I1
then
Result := 1
else
if Item1^.I1 < Item2^.I1
then
Result := -1
end;
function CompareByS1(
const Item1, Item2: PRecordTyp): integer;
begin
Result := 0;
if Item1^.S1 > Item2^.S1
then
Result := 1
else
if Item1^.S1 < Item2^.S1
then
Result := -1
end;
procedure AddItem(
const List: TList;
const I1: integer;
const S1:
string);
var
P: PRecordTyp;
begin
P := New(PRecordTyp);
P.I1 := I1;
P.S1 := S1;
List.Add(P);
end;
procedure DelItem(
const List: TList;
const Index: integer);
var
P: PRecordTyp;
begin
P := List.Items[
Index];
Dispose(P);
List.Delete(
Index);
end;
procedure FillList(
const List: Tlist);
const
N = 10000;
var
I, I1: integer;
S1:
string;
begin
for I := 1
to N
do
begin
I1 := Random(N);
S1 := IntToStr(Random(N));
AddItem(List, I1, S1);
end;
end;
procedure FillListBox(
const List: Tlist;
const ListBox: TListBox);
var
I: integer;
P: PRecordTyp;
begin
ListBox.Items.BeginUpdate;
ListBox.Clear;
try
for I := 0
to List.Count - 1
do
begin
P := List[I];
ListBox.Items.Add(IntToStr(P^.I1) + '
- ' + P^.S1);
end;
finally
ListBox.Items.EndUpdate;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
List: TList;
begin
List := TList.Create;
try
FillList(List);
List.Sort(@CompareByI1);
FillListBox(List, ListBox1);
while List.Count > 0
do
DelItem(List, List.Count - 1);
finally
List.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
List: TList;
begin
List := TList.Create;
try
FillList(List);
List.Sort(@CompareByS1);
FillListBox(List, ListBox1);
while List.Count > 0
do
DelItem(List, List.Count - 1);
finally
List.Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Randomize;
end;
end.