泛型排序器TComparer

表达式中的括号将严重影响分割结果.

uses RegularExpressions;

const
  FSourceText = ‘1: AAA 2: BBB 3: CCC’; // 分隔符将有三部分组成: 叁个数字, 多个冒号, 八个空格
var
  Arr: TArray;
  str: string;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Arr := TRegEx.Split(FSourceText, ‘d: ‘);

  Memo1.Clear;
  for str in Arr do Memo1.Lines.Add(str);
  {结果:

  AAA
  BBB
  CCC
  }
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Arr := TRegEx.Split(FSourceText, ‘(d卡塔尔: ‘State of Qatar; //括号中的部分被看作分割后的要素

  Memo1.Clear;
  for str in Arr do Memo1.Lines.Add(str);
  {

  1
  AAA
  2
  BBB
  3
  CCC
  }
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Arr := TRegEx.Split(FSourceText, ‘(d:卡塔尔国 ‘卡塔尔; // 证实下边

  Memo1.Clear;
  for str in Arr do Memo1.Lines.Add(str);
  {

  1:
  AAA
  2:
  BBB
  3:
  CCC
  }
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  Arr := TRegEx.Split(FSourceText, ‘(d卡塔尔(قطر‎(:卡塔尔(قطر‎ ‘State of Qatar; // 假设有五个括号, 取前边括号的剧情做元素

  Memo1.Clear;
  for str in Arr do Memo1.Lines.Add(str);
  {

  :
  AAA
  :
  BBB
  :
  CCC
  }
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  Arr := TRegEx.Split(FSourceText, ‘(d卡塔尔国(:卡塔尔(قطر‎( 卡塔尔’State of Qatar; // 证实上边

  Memo1.Clear;
  for str in Arr do Memo1.Lines.Add(str);
  {

  AAA

  BBB

  CCC
  }

测量检验代码文件:

 
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses Generics.Collections, Generics.Defaults; {务必的泛型单元}

//创设由 10 个整数组成的动态数组, 然后排序
procedure TForm1.Button1Click(Sender: TObject);
var
  arr: array of Integer;
  i: Integer;
begin
  {营造动态数组}
  Randomize;
  for i := 0 to 9 do begin
    SetLength(arr, Length(arr)+1);
    arr[i] := Random(10);
  end;

  {显示排序前}
  Memo1.Clear;
  for i := 0 to Length(arr) – 1 do Memo1.Lines.Add(IntToStr(arr[i]));

  {排序}
  TArray.Sort<Integer>(arrState of Qatar; {它能够有 1个、2个、4个参数,
这里只用了叁个参数, 其他是默许参数}

  {呈现排序结果}
  Memo2.Clear;
  for i := 0 to Length(arr) – 1 do
Memo2.Lines.Add(IntToStr(arr[i]));
end;

// TArray.Sort 的第2个参数是 IComparer<T> 类型, 其默许值是:
TComparer<T>.Default
procedure TForm1.Button2Click(Sender: TObject);
var
  arr: array of Integer;
  i: Integer;
begin
  Randomize;
  for i := 0 to 9 do begin
    SetLength(arr, Length(arr)+1);
    arr[i] := Random(10);
  end;

  Memo1.Clear;
  for i := 0 to Length(arr) – 1 do Memo1.Lines.Add(IntToStr(arr[i]));

  {使用了排序器的暗中认可值排序, 那和大体那个参数是同等的}
  TArray.Sort<Integer>(arr, TComparer<Integer>.Default);
  Memo2.Clear;
  for i := 0 to Length(arr) – 1 do
Memo2.Lines.Add(IntToStr(arr[i]));
end;

//同上两例, 变通了一下写法
procedure TForm1.Button3Click(Sender: TObject);
var
  arr: array of Integer;
  i: Integer;
  Comparer: IComparer<Integer>;
begin
  Randomize;
  for i := 0 to 9 do begin
    SetLength(arr, Length(arr)+1);
    arr[i] := Random(10);
  end;

  Memo1.Clear;
  for i := 0 to Length(arr) – 1 do Memo1.Lines.Add(IntToStr(arr[i]));

  Comparer := TComparer<Integer>.Default;
  TArray.Sort<Integer>(arr, Comparer);
  Memo2.Clear;
  for i := 0 to Length(arr) – 1 do
Memo2.Lines.Add(IntToStr(arr[i]));
end;

// TArray.Sort 的背后多少个参数能够钦点排序范围:
procedure TForm1.Button4Click(Sender: TObject);
var
  arr: array of Integer;
  i: Integer;
  Comparer: IComparer<Integer>;
begin
  Randomize;
  for i := 0 to 9 do begin
    SetLength(arr, Length(arr)+1);
    arr[i] := Random(10);
  end;

  Memo1.Clear;
  for i := 0 to Length(arr) – 1 do Memo1.Lines.Add(IntToStr(arr[i]));

  Comparer := TComparer<Integer>.Default;
  {这里钦点从第 1 个因素开首, 只给 3 个要素排序}
  TArray.Sort<Integer>(arr, Comparer, 0, 3);
  Memo2.Clear;
  for i := 0 to Length(arr) – 1 do
Memo2.Lines.Add(IntToStr(arr[i]));
end;

//要是要倒排序, 能够创设和煦的排序器, 上面正是先构建并贯彻了叁个TMyComparer, 然后调用:
type
  TMyComparer = class(TComparer<Integer>)
  public
    function Compare(const Left, Right: Integer): Integer; override;
  end;

{ TMyComparer }
function TMyComparer.Compare(const Left, Right: Integer): Integer;
begin
  Result := Right – Left;
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  arr: array of Integer;
  i: Integer;
  Comparer: TMyComparer;
begin
  Randomize;
  for i := 0 to 9 do begin
    SetLength(arr, Length(arr)+1);
    arr[i] := Random(10);
  end;

  Memo1.Clear;
  for i := 0 to Length(arr) – 1 do Memo1.Lines.Add(IntToStr(arr[i]));

  Comparer := TMyComparer.Create;
  TArray.Sort<Integer>(arr, Comparer);
  Memo2.Clear;
  for i := 0 to Length(arr) – 1 do
Memo2.Lines.Add(IntToStr(arr[i]));
  Comparer.Free;
end;

// 也得以用 TComparer<T>.Construct 方法, 通过两个 TComparison
格式的函数塑造排序器, 那样总结一些
function MyFunc1(const Left, Right: Integer): Integer;
begin
  Result := Right – Left;
end;

procedure TForm1.Button6Click(Sender: TObject);
var
  arr: array of Integer;
  i: Integer;
  Comparer: IComparer<Integer>;
begin
  Randomize;
  for i := 0 to 9 do begin
    SetLength(arr, Length(arr)+1);
    arr[i] := Random(10);
  end;

  Memo1.Clear;
  for i := 0 to Length(arr) – 1 do Memo1.Lines.Add(IntToStr(arr[i]));

  Comparer := TComparer<Integer>.Construct(MyFunc1);
  TArray.Sort<Integer>(arr, Comparer);
  Memo2.Clear;
  for i := 0 to Length(arr) – 1 do
Memo2.Lines.Add(IntToStr(arr[i]));
end;

//假诺是给自定义类型的要素排序, 只好是自建排序器
type
  TPerson = record
    name: string;
    age: Word;
  end;

function MyFunc2(const Left, Right: TPerson): Integer;
begin
  Result := Left.age – Right.age;
end;

procedure TForm1.Button7Click(Sender: TObject);
var
  arr: array of TPerson;
  i: Integer;
  Comparer: IComparer<TPerson>;
begin
  SetLength(arr, 4);
  arr[0].name := ‘AA’; arr[0].age := 22;
  arr[1].name := ‘BB’; arr[1].age := 33;
  arr[2].name := ‘CC’; arr[2].age := 44;
  arr[3].name := ‘DD’; arr[3].age := 11;

  Memo1.Clear;
  for i := 0 to Length(arr) – 1 do
    Memo1.Lines.Add(Format(‘%s : %d’, [arr[i].name, arr[i].age]));

  Comparer := TComparer<TPerson>.Construct(MyFunc2);
  TArray.Sort<TPerson>(arr, Comparer);
  Memo2.Clear;
  for i := 0 to Length(arr) – 1 do
    Memo2.Lines.Add(Format(‘%s : %d’, [arr[i].name,
arr[i].age]));
end;

// TStringComparer.Ordinal 是法定完成的用于 string 的排序器,
可平素使用.
//但它好像有标题(Delphi 2008 – 14.0.3513.24210State of Qatar,
以往的本子应该能修改来.
procedure TForm1.Button8Click(Sender: TObject);
var
  arr: array of string;
  i: Integer;
begin
  SetLength(arr, 4);
  arr[0] := ‘222’;
  arr[1] := ‘111’;
  arr[2] := ‘bbb’;
  arr[3] := ‘aaa’;

  Memo1.Clear;
  for i := 0 to Length(arr) – 1 do Memo1.Lines.Add(arr[i]);

  TArray.Sort<string>(arr, TStringComparer.Ordinal);
  Memo2.Clear;
  for i := 0 to Length(arr) – 1 do Memo2.Lines.Add(arr[i]);
end;

//下边是落到实处字符串数组的到排序
function MyComparerFunc(const s1,s2: string): Integer;
begin
  Result := CompareText(s2, s1);
  //Result := -CompareText(s1, s2卡塔尔国; {只怕那样}
end;

procedure TForm1.Button9Click(Sender: TObject);
var
  arr: array of string;
  i: Integer;
  Comparer: IComparer<string>;
begin
  Memo1.Clear;
  Memo1.Lines.CommaText := ‘11,33,22,AAA,CCC,BBB’;
  SetLength(arr, Memo1.Lines.Count);
  for i := 0 to Length(arr) – 1 do arr[i] := Memo1.Lines[i];

  Comparer := TComparer<string>.Construct(MyComparerFunc);
  TArray.Sort<string>(arr, Comparer);

  Memo2.Clear;
  for i := 0 to Length(arr) – 1 do Memo2.Lines.Add(arr[i]);
end;

//能够把上一个程序简写为:
procedure TForm1.Button10Click(Sender: TObject);
var
  arr: array of string;
  i: Integer;
  Comparer: IComparer<string>;
begin
  Memo1.Clear;
  Memo1.Lines.CommaText := ‘11,33,22,AAA,CCC,BBB’;
  SetLength(arr, Memo1.Lines.Count);
  for i := 0 to Length(arr) – 1 do arr[i] := Memo1.Lines[i];

  Comparer := TComparer<string>.Construct(
    function (const s1,s2: string): Integer
    begin
      Result := CompareText(s2, s1);
    end);
  TArray.Sort<string>(arr, Comparer);

  Memo2.Clear;
  for i := 0 to Length(arr) – 1 do Memo2.Lines.Add(arr[i]);
end;

end.

 

delphi 关于 TRegEx.Split()

发表评论

电子邮件地址不会被公开。 必填项已用*标注