1 unit Unit1; 2 3 interface 4 5 uses 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, StdCtrls; 8 9 type10 TForm1 = class(TForm)11 Button1: TButton;12 procedure Button1Click(Sender: TObject);13 end;14 15 TMyRec = record16 x: Integer;17 y: Integer;18 z: Integer;19 end;20 PMyRec = ^TMyRec;21 22 var23 Form1: TForm1;24 25 implementation26 27 { $R *.dfm}28 //function StringListCompareStrings(List: TStringList; Index1, Index2: Integer): Integer;29 30 function SortMethod(List: TStringList; Index1, Index2: Integer): Integer;31 begin32 if PMyRec(List.Objects[Index1])^.x > PMyRec(List.Objects[Index2])^.x then33 Result := -134 else35 if PMyRec(List.Objects[Index1])^.x = PMyRec(List.Objects[Index2])^.x then36 Result := 037 else38 Result := 1;39 end;40 41 procedure TForm1.Button1Click(Sender: TObject);42 var43 strList: TStringList;44 mRec1, mRec2, mRec3: TMyRec;45 pRec1, pRec2, pRec3: PMyRec;46 begin47 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 do56 begin57 x := 1;58 y := 2;59 z := 3;60 end;61 62 with mRec2 do63 begin64 x := 3;65 y := 2;66 z := 3;67 end;68 69 with mRec3 do70 begin71 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 ) //383 + IntToStr( PMyRec(strList.Objects[1])^.x ) //184 + IntToStr( PMyRec(strList.Objects[2])^.x ) //585 );86 87 strList.CustomSort(SortMethod);88 89 ShowMessage( IntToStr( PMyRec(strList.Objects[0])^.x ) //590 + IntToStr( PMyRec(strList.Objects[1])^.x ) //391 + IntToStr( PMyRec(strList.Objects[2])^.x ) //192 );93 94 strList.Free;95 96 97 end;98 99 end.
unit Unit1;
interfaceuses 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.
posted on 2016-04-12 20:50 阅读( ...) 评论( ...)