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.

posted on 2009-10-09 21:29  万一  阅读(5464)  评论(3编辑  收藏  举报