AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Integer Variablen sortieren

Ein Thema von DelphiUser123 · begonnen am 21. Mai 2024 · letzter Beitrag vom 22. Mai 2024
Antwort Antwort
Blup

Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.487 Beiträge
 
Delphi 12 Athens
 
#1

AW: Integer Variablen sortieren

  Alt 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.
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 04:59 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz