Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#26

AW: Sortieren mit direktem Auswahlort

  Alt 7. Dez 2014, 18:22
Wenn wir schon beim eleganter sind, dann sowas in der Art:
Delphi-Quellcode:
unit ValueSorter;

interface

type
  TValueSorter = class
  private
    FValues: array of Integer;
    FCurrentValue: string;
    FValue: Integer;
    FValuesAsString: string;
    procedure SetCurrentValue( const Value: string );
    function GetValue( index: Integer ): Integer;
    function GetValueCount: Integer;
  protected
    // diese Methoden verrichten die eigentliche Arbeit
    procedure DoAddValue;
    procedure DoSort;
    procedure DoReset;
    procedure DoUpdateValuesAsString;
  public
    // Commands
    function CanAddValue: Boolean;
    procedure AddValue;

    function CanSort: Boolean;
    procedure Sort;

    function CanReset: Boolean;
    procedure Reset;

    // Eigenschaften
    property CurrentValue: string read FCurrentValue write SetCurrentValue;
    property ValueCount: Integer read GetValueCount;
    property Values[index: Integer]: Integer read GetValue;
    property ValuesAsString: string read FValuesAsString;
  end;

implementation

uses
  System.SysUtils;

{ TValueSorter }

procedure TValueSorter.AddValue;
begin
  if CanAddValue
  then
    DoAddValue;
end;

function TValueSorter.CanAddValue: Boolean;
begin
  Result := TryStrToInt( FCurrentValue, FValue );
end;

function TValueSorter.CanReset: Boolean;
begin
  Result := Length( FValues ) > 0;
end;

function TValueSorter.CanSort: Boolean;
begin
  Result := Length( FValues ) > 1;
end;

procedure TValueSorter.DoAddValue;
begin
  SetLength( FValues, Length( FValues ) + 1 );
  FValues[High( FValues )] := FValue;
  FCurrentValue := '';
  DoUpdateValuesAsString;
end;

procedure TValueSorter.DoReset;
begin
  SetLength( FValues, 0 );
  DoUpdateValuesAsString;
end;

procedure TValueSorter.DoSort;
var
  LIIdx, LJIdx, LMinIdx: Integer;
  LTmp: Integer;
begin
  For LIIdx := Low( FValues ) to High( FValues ) - 1 Do
    Begin
      LMinIdx := LIIdx;
      For LJIdx := LIIdx + 1 To High( FValues ) Do
        If ( FValues[LJIdx] < FValues[LMinIdx] )
        Then
          begin
            LTmp := FValues[LJIdx];
            FValues[LJIdx] := FValues[LMinIdx];
            FValues[LMinIdx] := LTmp;
          end;
    end;
  DoUpdateValuesAsString;
end;

procedure TValueSorter.DoUpdateValuesAsString;
var
  LIdx: Integer;
begin
  FValuesAsString := '';
  for LIdx := Low( FValues ) to High( FValues ) do
    begin
      if LIdx > Low( FValues )
      then
        FValuesAsString := FValuesAsString + ';';
      FValuesAsString := FValuesAsString + IntToStr( FValues[LIdx] );
    end;
end;

function TValueSorter.GetValue( index: Integer ): Integer;
begin
  Result := FValues[Index + Low( FValues )];
end;

function TValueSorter.GetValueCount: Integer;
begin
  Result := Length( FValues );
end;

procedure TValueSorter.Reset;
begin
  if CanReset
  then
    DoReset;
end;

procedure TValueSorter.SetCurrentValue( const Value: string );
begin
  FCurrentValue := Value;
end;

procedure TValueSorter.Sort;
begin
  if CanSort
  then
    DoSort;
end;

end.
Delphi-Quellcode:
unit Form.Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Actions,
  Vcl.ActnList, Vcl.AppEvnts,

  ValueSorter;

type
  TForm1 = class( TForm )
    ValueEdit: TEdit;
    AddButton: TButton;
    SortButton: TButton;
    ResetButton: TButton;
    Label1: TLabel;
    ApplicationEvents1: TApplicationEvents;
    procedure ApplicationEvents1Idle( Sender: TObject; var Done: Boolean );
    procedure ValueEditChange( Sender: TObject );
    procedure AddButtonClick( Sender: TObject );
    procedure SortButtonClick( Sender: TObject );
    procedure ResetButtonClick( Sender: TObject );
  private
    FValueSorter: TValueSorter;
  public
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
{ TForm1 }

procedure TForm1.AddButtonClick( Sender: TObject );
begin
  FValueSorter.AddValue;
end;

procedure TForm1.AfterConstruction;
begin
  inherited;
  FValueSorter := TValueSorter.Create;
end;

procedure TForm1.ApplicationEvents1Idle( Sender: TObject; var Done: Boolean );
begin
  // Aktualisierung der Anzeige
  ValueEdit.Text := FValueSorter.CurrentValue;
  Label1.Caption := FValueSorter.ValuesAsString;
  AddButton.Enabled := FValueSorter.CanAddValue;
  SortButton.Enabled := FValueSorter.CanSort;
  ResetButton.Enabled := FValueSorter.CanReset;
end;

procedure TForm1.BeforeDestruction;
begin
  FValueSorter.Free;
  inherited;
end;

procedure TForm1.ResetButtonClick( Sender: TObject );
begin
  FValueSorter.Reset;
end;

procedure TForm1.SortButtonClick( Sender: TObject );
begin
  FValueSorter.Sort;
end;

procedure TForm1.ValueEditChange( Sender: TObject );
begin
  // Aktualisierung der Benutzereingabe
  FValueSorter.CurrentValue := ValueEdit.Text;
end;

end.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)

Geändert von Sir Rufo ( 7. Dez 2014 um 18:24 Uhr)
  Mit Zitat antworten Zitat