unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses Generics.Collections; {引用泛型單元}
{給字符串數組排序}
procedure TForm1.Button1Click(Sender: TObject);
var
arr: array of string;
i: Integer;
begin
{設置動態數組大小}
SetLength(arr, Memo1.Lines.Count);
{復制 Memo1 中的文本到數組}
for i := 0 to Memo1.Lines.Count - 1 do arr[i] := Memo1.Lines[i];
{排序}
TArray.Sort<string>(arr);
{在 Memo1 中查看數組中的數據}
Memo1.Clear;
for i := 0 to Length(arr) - 1 do Memo1.Lines.Add(arr[i]);
end;
{給整數數組排序}
procedure TForm1.Button2Click(Sender: TObject);
const
num = 10;
var
arr: array of Integer;
i: Integer;
begin
SetLength(arr, num);
Randomize;
Memo1.Clear;
{把 10 個隨機數放入數組, 並顯示在 Memo1 中}
for i := 0 to num - 1 do
begin
arr[i] := Random(100);
Memo1.Lines.Add(IntToStr(arr[i]));
end;
{排序}
TArray.Sort<Integer>(arr);
{等 1 秒中後查看排序結果}
Sleep(1000);
Memo1.Clear;
for i := 0 to num - 1 do Memo1.Lines.Add(IntToStr(arr[i]));
end;
end.