Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.464 Beiträge
Delphi 12 Athens
|
AW: Integer Variablen sortieren
22. Mai 2024, 13:00
Um Variablen-Namen sortiert nach deren Inhalt und mit dem Inhalt als Liste auszugeben, muss man so eine Liste erst einmal erzeugen.
Delphi-Quellcode:
uses
System.Classes, Generics.Collections, Generics.Defaults;
type
TMyValue = Integer;
PMyValue = ^TMyValue;
TMyVariable = class(TObject)
private
P: PMyValue;
function GetValue: TMyValue;
procedure SetValue(const AValue: TMyValue);
public
Name: string;
constructor Create(const AName: string; var AValue: TMyValue);
property Value: TMyValue read GetValue write SetValue;
end;
TMyVariableComparer = class(TComparer<TMyVariable>)
function Compare(const Left, Right: TMyVariable): Integer; override;
end;
TMyVariableList = TObjectList<TMyVariable>;
procedure InitMyVariableList;
procedure RandomizeMyVariableList;
procedure SortMyVariableList;
procedure WriteMyVariableList;
var
L1, L2, L3, L4: TMyValue;
implementation
var
MyVariableList: TMyVariableList;
procedure InitMyVariableList;
begin
FreeAndNil(MyVariableList);
MyVariableList := TMyVariableList.Create;
MyVariableList.Add(TMyVariable.Create('L1', L1));
MyVariableList.Add(TMyVariable.Create('L2', L2));
MyVariableList.Add(TMyVariable.Create('L3', L3));
MyVariableList.Add(TMyVariable.Create('L4', L4));
end;
procedure RandomizeMyVariableList;
begin
Randomize;
for var lItem in MyVariableList do
lItem.Value := Random(MaxInt);
end;
procedure SortMyVariableList;
begin
MyVariableList.Sort(TMyVariableComparer.Create);
end;
procedure WriteMyVariableList;
begin
{ TODO : }
end;
{ TMyVariable }
constructor TMyVariable.Create(const AName: string; var AValue: TMyValue);
begin
Name := AName;
P := @AValue;
end;
function TMyVariable.GetValue: TMyValue;
begin
Result := P^;
end;
procedure TMyVariable.SetValue(const AValue: TMyValue);
begin
P^ := AValue;
end;
{ TMyVariableComparer }
function TMyVariableComparer.Compare(const Left, Right: TMyVariable): Integer;
begin
Result := Left.Value - Right.Value;
end;
finalization
MyVariableList.Free;
end.
|
|
Zitat
|