Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.522 Beiträge
Delphi 12 Athens
|
AW: Unterschiedliche Ergebnisse TList.Sort 32bit / 64bit CompareItems
16. Mai 2023, 14:26
Entweder so:
Delphi-Quellcode:
type
PMyStruct = ^TMyStruct;
TMyStruct = packed record
Value1 : LongInt;
Value2 : Double;
Value3 : Byte;
class function Compare(const A, B: TMyStruct): TValueRelationship; static;
function GetDataStr: String;
end;
class function TMyStruct.Compare(const A, B: TMyStruct): TValueRelationship;
begin
Result := CompareValue(A.Value1, B.Value1);
if Result <> 0 then
exit;
Result := CompareValue(A.Value2, B.Value2);
if Result <> 0 then
exit;
Result := CompareValue(A.Value3, B.Value3);
end;
function CompareItems(PItem1,PItem2: Pointer): Integer;
begin
Result := TMyStruct.Compare(PMyStruct(PItem1)^, PMyStruct(PItem2)^);
end;
...
aList.Sort(CompareItems);
...
Oder gleich mit einer generischen Liste (Das spart übrigens auch die Freigabe der Records):
Delphi-Quellcode:
type
TMyStruct = packed record
Value1 : LongInt;
Value2 : Double;
Value3 : Byte;
constructor Create(const AValue1: LongInt; const AValue2: Double; const AValue3: Byte);
class function Compare(const A, B: TMyStruct): Integer; static;
function GetDataStr: String;
end;
TMyStructList = class(TList<TMyStruct>)
public
constructor Create;
end;
constructor TMyStruct.Create(const AValue1: LongInt; const AValue2: Double; const AValue3: Byte);
begin
Value1 := AValue1;
Value2 := AValue2;
Value3 := AValue3;
end;
class function TMyStruct.Compare(const A, B: TMyStruct): Integer;
begin
Result := CompareValue(A.Value1, B.Value1);
if Result <> 0 then
exit;
Result := CompareValue(A.Value2, B.Value2);
if Result <> 0 then
exit;
Result := CompareValue(A.Value3, B.Value3);
end;
function TMyStruct.GetDataStr: String;
begin
Result := Value1.ToString + '/' + FloatToStrF(Value2, ffFixed, 15, 3) + '/' + Value3.ToString;
end;
constructor TMyStructList.Create;
begin
inherited Create(TComparer<TMyStruct>.Construct(TMyStruct.Compare));
end;
...
var aList := TMyStructList.Create;
try
for var i := 0 to 5 do
aList.Add(TMyStruct.Create(1429078, 6.220, i));
for var i := 0 to 5 do
aList.Add(TMyStruct.Create(1429079,6.220, i));
for var i := 0 to 5 do
aList.Add(TMyStruct.Create(1429080,6.220, i));
for var i := 0 to 0 do
aList.Add(TMyStruct.Create(1429081,6.220, i));
for var i := 0 to 1 do
aList.Add(TMyStruct.Create(1429081,5.810, i));
for var i := 0 to 4 do
aList.Add(TMyStruct.Create(1429082,5.810, i));
for var i := 0 to 5 do
aList.Add(TMyStruct.Create(1429083,5.810, i));
for var i := 0 to 5 do
aList.Add(TMyStruct.Create(1429084,5.810, i));
Memo1.Clear;
Memo1.Lines.Add('VOR Sortierung');
for var i := 0 to aList.Count - 1 do
begin
var aData := aList.Items[i];
Memo1.Lines.Add('(' + i.ToString +') - aData: ' + aData.GetDataStr);
end;
aList.Sort;
Memo2.Clear;
Memo2.Lines.Add('NACH Sortierung');
for var i := 0 to aList.Count - 1 do
begin
var aData := aList.Items[i];
Memo2.Lines.Add('(' + i.ToString +') - aData: ' + aData.GetDataStr);
end;
finally
FreeAndNil(aList);
end;
...
|
|
Zitat
|