![]() |
Custom Sort - CompareItems mit Boolean
Hi ihr,
irgendwie steh ich grad voll auf dem Schlauch...
Delphi-Quellcode:
Jetzt hab ich in meiner Klasse auch Felder vom Typ Boolean.
function TMyClass.CompareItems(Item1, Item2: Pointer): Integer;
Wie kann ich nun nach Boolean Sortieren? :wiejetzt: Sprich erst True dann False / vice versa |
Re: Custom Sort - CompareItems mit Boolean
Wo ist das Problem? Das ist doch dir überlassen, wie du deine Items sortierst. Ich würde einfach False < True verwenden, da False als 0 und True als 1 deklariert ist, und 0 < 1 ist.
Delphi-Quellcode:
Ich weiß jetzt nicht, ob 1 und -1 stimmt, vielleicht muss es auch andersrum sein, um meiner oben genannten Sortierlogik zu entsprechen - ich verwechsel immer, wofür positiv und negativ beim Sortieren steht.
if TMyItem(Item1).MyBool = TMyItem(Item2).MyBool then
Result := 0 else if TMyItem(Item1).MyBool and not TMyItem(Item2).MyBool then Result := 1 else{ if not TMyItem(Item1).MyBool and TMyItem(Item2).MyBool then} Result := -1 |
Re: Custom Sort - CompareItems mit Boolean
Delphi-Quellcode:
Hinweis: True ist nicht immer gleich True. Es kann unterschiedlich definiert sein (Boolean, ByteBool, WordBool,... in C, oder etwa bei Datenbanken). Einmal sei True = -1, ein andermal sei True = 1, dann ergeben sich für Cardinal(True), Integer(True) andere Werte, und dehen ggf. die Relation False < True um. Vielleicht hilft es Integer(Boolean(...)) zu schreiben (?).
function Compare(const Left, Right: T): Integer;
begin Result := Integer(Left.BooleanValue) - Integer(Right.BooleanValue); // False < True end; |
Re: Custom Sort - CompareItems mit Boolean
Da geh ich dann doch lieber auf Nummer Sicher und nutze meine Methode :wink:
|
Re: Custom Sort - CompareItems mit Boolean
Zitat:
Delphi-Quellcode:
oder so:
Function CompareBoolean (a,b : Boolean) : Integer;
Begin Result := ord(a) - ord(b); End;
Delphi-Quellcode:
Function CompareBoolean (a,b : Boolean) : Integer;
Const BoolOrder : Array [False..True] Of Integer = (0,1); // Oder 1,0 wenn man andersherum sortieren will Begin Result := BoolOrder[a] - BoolOrder[b]; End; |
Re: Custom Sort - CompareItems mit Boolean
Danke für eure Hilfe nun sollte es klappen :dp:
|
Re: Custom Sort - CompareItems mit Boolean
Statt
Zitat:
Delphi-Quellcode:
schreiben.
BoolOrder : Array [Boolean] Of Integer = (0,1);
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:43 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