StringList 自定义快速排序

时间:2022-04-17 07:08:14
 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 := -
else
if PMyRec(List.Objects[Index1])^.x = PMyRec(List.Objects[Index2])^.x then
Result :=
else
Result := ;
end; procedure TForm1.Button1Click(Sender: TObject);
var
strList: TStringList;
mRec1, mRec2, mRec3: TMyRec;
pRec1, pRec2, pRec3: PMyRec;
begin
FillChar(mRec1, SizeOf(TMyRec), );
FillChar(mRec2, SizeOf(TMyRec), );
FillChar(mRec3, SizeOf(TMyRec), ); pRec1 := @mRec1;
pRec2 := @mRec2;
pRec3 := @mRec3; with mRec1 do
begin
x := ;
y := ;
z := ;
end; with mRec2 do
begin
x := ;
y := ;
z := ;
end; with mRec3 do
begin
x := ;
y := ;
z := ;
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[])^.x ) //
+ IntToStr( PMyRec(strList.Objects[])^.x ) //
+ IntToStr( PMyRec(strList.Objects[])^.x ) //
); strList.CustomSort(SortMethod); ShowMessage( IntToStr( PMyRec(strList.Objects[])^.x ) //
+ IntToStr( PMyRec(strList.Objects[])^.x ) //
+ IntToStr( PMyRec(strList.Objects[])^.x ) //
); strList.Free; end; 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.