StringList 自定义快速排序

 1 unit Unit1;
 2 
 3 interface
 4 
 5 uses
 6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7   Dialogs, StdCtrls;
 8 
 9 type
10   TForm1 = class(TForm)
11     Button1: TButton;
12     procedure Button1Click(Sender: TObject);
13   end;
14 
15     TMyRec = record
16     x: Integer;
17     y: Integer;
18     z: Integer;
19   end;
20   PMyRec = ^TMyRec;
21 
22 var
23   Form1: TForm1;
24 
25 implementation
26 
27 {$R *.dfm}
28 //function StringListCompareStrings(List: TStringList; Index1, Index2: Integer): Integer;
29 
30 function SortMethod(List: TStringList; Index1, Index2: Integer): Integer;
31 begin
32   if PMyRec(List.Objects[Index1])^.x > PMyRec(List.Objects[Index2])^.x then
33     Result := -1
34   else
35   if PMyRec(List.Objects[Index1])^.x = PMyRec(List.Objects[Index2])^.x then
36     Result := 0
37   else
38     Result := 1;
39 end;
40 
41 procedure TForm1.Button1Click(Sender: TObject);
42 var
43   strList: TStringList;
44   mRec1, mRec2, mRec3: TMyRec;
45   pRec1, pRec2, pRec3: PMyRec;
46 begin
47   FillChar(mRec1, SizeOf(TMyRec), 0);
48   FillChar(mRec2, SizeOf(TMyRec), 0);
49   FillChar(mRec3, SizeOf(TMyRec), 0);
50 
51   pRec1 := @mRec1;
52   pRec2 := @mRec2;
53   pRec3 := @mRec3;
54 
55   with mRec1 do
56   begin
57     x := 1;
58     y := 2;
59     z := 3;
60   end;
61 
62   with mRec2 do
63   begin
64     x := 3;
65     y := 2;
66     z := 3;
67   end;
68 
69   with mRec3 do
70   begin
71     x := 5;
72     y := 2;
73     z := 3;
74   end;
75 
76   strList := TStringList.Create;
77 
78   strList.AddObject(IntToStr(pRec1^.x), TObject(pRec2));     
79   strList.AddObject(IntToStr(pRec1^.x), TObject(pRec1));     
80   strList.AddObject(IntToStr(pRec1^.x), TObject(pRec3));
81 
82   ShowMessage( IntToStr(   PMyRec(strList.Objects[0])^.x )       //3
83   + IntToStr(   PMyRec(strList.Objects[1])^.x )                  //1
84   + IntToStr(   PMyRec(strList.Objects[2])^.x )                  //5
85   );
86 
87   strList.CustomSort(SortMethod);
88 
89   ShowMessage( IntToStr(   PMyRec(strList.Objects[0])^.x )       //5
90   + IntToStr(   PMyRec(strList.Objects[1])^.x )                  //3
91   + IntToStr(   PMyRec(strList.Objects[2])^.x )                  //1
92   );
93 
94   strList.Free;
95 
96 
97 end;
98 
99 end.

unit Unit1;
interface
uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls;
type  TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  end;
    TMyRec = record    x: Integer;    y: Integer;    z: Integer;  end;  PMyRec = ^TMyRec;
var  Form1: TForm1;
implementation
{$R *.dfm}//function StringListCompareStrings(List: TStringList; Index1, Index2: Integer): Integer;
function SortMethod(List: TStringList; Index1, Index2: Integer): Integer;begin  if PMyRec(List.Objects[Index1])^.x > PMyRec(List.Objects[Index2])^.x then    Result := -1  else  if PMyRec(List.Objects[Index1])^.x = PMyRec(List.Objects[Index2])^.x then    Result := 0  else    Result := 1;end;
procedure TForm1.Button1Click(Sender: TObject);var  strList: TStringList;  mRec1, mRec2, mRec3: TMyRec;  pRec1, pRec2, pRec3: PMyRec;begin  FillChar(mRec1, SizeOf(TMyRec), 0);  FillChar(mRec2, SizeOf(TMyRec), 0);  FillChar(mRec3, SizeOf(TMyRec), 0);
  pRec1 := @mRec1;  pRec2 := @mRec2;  pRec3 := @mRec3;
  with mRec1 do  begin    x := 1;    y := 2;    z := 3;  end;
  with mRec2 do  begin    x := 3;    y := 2;    z := 3;  end;
  with mRec3 do  begin    x := 5;    y := 2;    z := 3;  end;
  strList := TStringList.Create;
  strList.AddObject(IntToStr(pRec1^.x), TObject(pRec2));       strList.AddObject(IntToStr(pRec1^.x), TObject(pRec1));       strList.AddObject(IntToStr(pRec1^.x), TObject(pRec3));
  ShowMessage( IntToStr(   PMyRec(strList.Objects[0])^.x )       //3  + IntToStr(   PMyRec(strList.Objects[1])^.x )                  //1  + IntToStr(   PMyRec(strList.Objects[2])^.x )                  //5  );
  strList.CustomSort(SortMethod);
  ShowMessage( IntToStr(   PMyRec(strList.Objects[0])^.x )       //5  + IntToStr(   PMyRec(strList.Objects[1])^.x )                  //3  + IntToStr(   PMyRec(strList.Objects[2])^.x )                  //1  );
  strList.Free;

end;
end.

原文地址:https://www.cnblogs.com/devinblog/p/5384392.html