Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Delphi TObjectList Sortieren (https://www.delphipraxis.net/199917-tobjectlist-sortieren.html)

franktron 1. Mär 2019 16:17

TObjectList Sortieren
 
Ich habe ein Problem beim Sortieren einer Klasse

Delphi-Quellcode:
  TAnfrage_Sort = class(TComparer<TAnfrageItem>)
    function Compare(Const Left,Right : TAnfrageItem): Integer; override;
  end;
  TAnfrageItem = class
  private
    function GetSortInt: LongInt;
    .....
  public
    property ID : LongInt read FID write SetID;
    property TypID : Longint read FTypID write SetTypID;
    property Bemerkung : TStringList read FBemerkung write SetBemerkung;
    property isJa : Boolean read FisJa write SetisJa;
    property Text : String read FText write SetText;
    property Farbe : TAlphaColor read FFarbe write SetFarbe;
    property GruppeID : Longint read FGruppeID write SetGruppeID;
    property Sort : Longint read FSort write SetSort;
    property SortGruppe : Longint read FSortGruppe write SetSortGruppe;
    property SortInt : LongInt read GetSortInt;

    constructor Create;
    destructor Destroy; override;
  end;
 
function TAnfrageItem.GetSortInt: LongInt;
begin
  Result:=StrToInt(FormatFloat('0000',FSortGruppe)+FormatFloat('0000',FSort));
end;


Function TAnfrage_Sort.Compare(const Left,
  Right: TAnfrageItem): Integer;
var
  i  : Integer;
begin
  i:=0;
  if Left.SortInt>Right.SortInt then
  begin
    i:=1;
  end;
  if Left.SortInt<Right.SortInt then
  begin
    i:=-1;
  end;
  Result:=i;
end;

Hier der Aufruf
procedure TAnfrage.Sort;
var
  LPC : IComparer<TAnfrageItem>;
begin
  LPC:=TAnfrage_Sort.Create;
  FItems.Sort(LPC);
end;
Ich will 2 Felder Sortieren also SortGruppe,Sort
aber irgendwie klappt das Sortieren gar nicht es kommt nur Blödsinn raus.

SortGruppe ist immer richtig Sort leider nicht.

Was mache ich da Falsch

freimatz 1. Mär 2019 16:18

AW: TObjectList Sortieren
 
Debuggen?

Union 1. Mär 2019 16:22

AW: TObjectList Sortieren
 
Also entweder multiplizieren oder Du sortierst den String.

Delphi-Quellcode:
function TAnfrageItem.GetSortInt: LongInt;
begin
  Result:=StrToInt(FormatFloat('0000',FSortGruppe)*10000+FormatFloat('0000',FSort));
end;

Uwe Raabe 1. Mär 2019 16:57

AW: TObjectList Sortieren
 
Warum so kompliziert mit SortInt?
Delphi-Quellcode:
function TAnfrage_Sort.Compare(const Left, Right: TAnfrageItem): Integer;
begin
  Result := CompareValue(Left.SortGruppe, Right.SortGruppe);
  if Result = 0 then
    Result := CompareValue(Left.Sort, Right.Sort);
end;
Das CompareValue findest du in System.Math.


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:44 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-2025 by Thomas Breitkreuz