procedure Swap<t>(var A, B: t); begin var C: t := A; A := B; B := C; end; //
procedure Sortarr<T1, T2>(var SourceArr: TList<T1>; var SortArr: TList<T2>; ascending: Boolean); var i, j: Integer; begin if ascending then begin for i := 0 to SourceArr.Count - 2 do for j := 0 to SourceArr.Count - 2 - i do begin if TValue.From<t1>(SourceArr[j]).tostring > TValue.From<t1>(SourceArr[j + 1]).tostring then begin Swap<t1>(SourceArr[j], SourceArr[j + 1]); Swap<t2>(SortArr[j], SortArr[j + 1]); end; end; end else begin for i := 0 to SourceArr.Count - 2 do for j := 0 to SourceArr.Count - 2 - i do begin if TValue.From<t1>(SourceArr[j]).tostring < TValue.From<t1>(SourceArr[j + 1]).tostring then begin Swap<t1>(SourceArr[j], SourceArr[j + 1]); Swap<t2>(SortArr[j], SortArr[j + 1]); end; end; end; end; //请问这样写为什么swap那里报错,如果把Tlist换成Tarray就不会报错
----------------------------------------------
-
xxxxx( @obj1, @obj2 ); --> "@" -> pointer of the objeto passed
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
var = object REFERENCE... then, it will be the object itself
"inline" < 32bytes if im not wrong ...... the "code" would be placed in all places where the func/proc was used... in a regular func/proc, only the "reference/pointer" would be used (+/-) this app size with many "inline" proc/func = > size
NOTE: there is not certainties about it, because depend of many things and the compiler may or may not be able to embed code in all places in the code where the function or procedure was used... the many conditions that will be evaluated
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
//class procedure TMyClass<T>.SortMyArr(var Arr: TArr; const Comparer: IComparer<T> = nil); class procedure TMyClass<T>.SortMyArr(var Arr: TArr); var Ctx: TRttiContext; Typ: TRttiType; // LenArr: integer; begin LenArr := length(Arr); if (LenArr < 2) then exit; // Ctx := TRttiContext.Create; Typ := Ctx.GetType(TypeInfo(T)); // if (Typ <> nil) then begin case Typ.TypeKind of tkUString, tkInteger { simple types ... } : begin // TArray.Sort<T>(Arr, Comparer); TArray.Sort<T>(Arr); end; tkClass: begin // how to order it? end; // ... end; end; end;
procedure TForm1.Button2Click(Sender: TObject); var LArr : TArray<TButton>; LText: string; begin LArr := [TButton.Create(Form1), TButton.Create(Form1), TButton.Create(Form1), TButton.Create(Form1)]; LArr[0].Name := 'bt4'; LArr[1].Name := 'bt2'; LArr[2].Name := 'bt1'; LArr[3].Name := 'bt3'; // Memo1.Text := ''; // for var B in LArr do LText := LText + slinebreak + B.Name; // Memo1.Lines.Add(LText); // TMyClass<TButton>.SortMyArr(LArr); // Memo1.Lines.Add('--------'); // LText := ''; for var B in LArr do LText := LText + slinebreak + B.Name; // Memo1.Lines.Add(LText); end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3