Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Custom Sort - CompareItems mit Boolean (https://www.delphipraxis.net/147718-custom-sort-compareitems-mit-boolean.html)

ryLIX 14. Feb 2010 22:57


Custom Sort - CompareItems mit Boolean
 
Hi ihr,
irgendwie steh ich grad voll auf dem Schlauch...
Delphi-Quellcode:
function TMyClass.CompareItems(Item1, Item2: Pointer): Integer;
Jetzt hab ich in meiner Klasse auch Felder vom Typ Boolean.
Wie kann ich nun nach Boolean Sortieren? :wiejetzt:
Sprich erst True dann False / vice versa

Namenloser 14. Feb 2010 23:05

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:
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
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.

Panthrax 15. Feb 2010 00:05

Re: Custom Sort - CompareItems mit Boolean
 
Delphi-Quellcode:
function Compare(const Left, Right: T): Integer;
begin
  Result := Integer(Left.BooleanValue) - Integer(Right.BooleanValue); // False < True
end;
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 (?).

Namenloser 15. Feb 2010 00:48

Re: Custom Sort - CompareItems mit Boolean
 
Da geh ich dann doch lieber auf Nummer Sicher und nutze meine Methode :wink:

alzaimar 15. Feb 2010 06:22

Re: Custom Sort - CompareItems mit Boolean
 
Zitat:

Zitat von NamenLozer
Da geh ich dann doch lieber auf Nummer Sicher und nutze meine Methode :wink:

Is aber unübersichtlich und langsam(er), dann lieber so:
Delphi-Quellcode:
Function CompareBoolean (a,b : Boolean) : Integer;
Begin
  Result := ord(a) - ord(b);
End;
oder so:
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;

ryLIX 15. Feb 2010 09:22

Re: Custom Sort - CompareItems mit Boolean
 
Danke für eure Hilfe nun sollte es klappen :dp:

DeddyH 15. Feb 2010 09:27

Re: Custom Sort - CompareItems mit Boolean
 
Statt
Zitat:

Delphi-Quellcode:
BoolOrder : Array [False..True] Of Integer = (0,1);

würde ich aber gleich
Delphi-Quellcode:
BoolOrder : Array [Boolean] Of Integer = (0,1);
schreiben.


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:00 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz