Code: Alles auswählen
program testproject;
{$mode objfpc}{$H+}
uses
Classes, SysUtils, Generics.Collections, Generics.Defaults, Math;
type
{ TStressEntry }
TStressEntry = class
ID: integer;
stress: double;
end;
TStressList = specialize TObjectList<TStressEntry>;
var
i: integer;
NodeID: integer;
Stress: double;
StrLst, Line: TstringList;
str: string;
StressEntry: TStressEntry;
StressList: TStressList;
L1, L2 : string;
function Compare(constref ALeft, ARight: TStressEntry): Integer;
begin
Result := ALeft.Id - ARight.Id;
end;
begin
StressList := TStressList.Create;
StrLst := TStringList.Create;
//Simulates LoadFromFile
StrLst.Add('513384 -4,821229171753e+01');
StrLst.Add('806394 -4,821158599854e+01');
StrLst.Add('513934 -4,778438568115e+01');
StrLst.Add('806944 -4,778303909302e+01');
StrLst.Add('809518 -4,754859924316e+01');
StrLst.Add('516512 -4,754709625244e+01');
StrLst.Add('517078 -4,745193862915e+01');
StrLst.Add('810084 -4,744944381714e+01');
StrLst.Add('513247 -4,690411376953e+01');
StrLst.Add('806257 -4,690361785889e+01');
StrLst.Add('513851 -4,689748382568e+01');
StrLst.Add('806861 -4,689499282837e+01');
StrLst.Add('513713 -4,639287948608e+01');
StrLst.Add('806723 -4,639037704468e+01');
StrLst.Add('514801 -4,603351974487e+01');
StrLst.Add('807807 -4,602876281738e+01');
StrLst.Add('515922 -4,540652465820e+01');
StrLst.Add('808928 -4,540190505981e+01');
for i:=0 to StrLst.Count-1 do
begin
str := StrLst[i];
Line := TStringList.Create;
Line.DelimitedText := str;
L1 := Line[0];
L2 := Line[1];
NodeID := StrToInt(L1);
Stress := StrToFloat(L2);
writeln(Format(' %2d: %6d -- %12.6f',[i,NodeID,Stress]));
StressEntry := TStressEntry.Create;
StressEntry.ID := NodeID;
StressEntry.stress := Stress;
StressList.Add(StressEntry);
Line.Free;
end;
writeln(Format(' length of StressList: %d',[StressList.Count]));
for i:=0 to StressList.Count-1 do
begin
writeln(Format(' StressList[%2d]: %6d -- %12.6f',
[i,StressList[i].ID,StressList[i].Stress]));
end;
// Now sort
StressList.Sort(specialize TComparer<TStressEntry>.Construct(@Compare));
//StressList.Sort(@CompareStressList); // <--- hier stoppt der Compiler
for i:=0 to StressList.Count-1 do
begin
writeln(Format('NOW SORTED - StressList[%2d]: %6d -- %12.6f',
[i,StressList[i].ID,StressList[i].Stress]));
end;
ReadLn;
end.