I am just starting to get my feet wet with this .
PNode = ^TNode;
TNode = record
Obstacle : boolean;
Visited : boolean;
GCost : double; // Distance from Starting Node
HCost : double; // Distance from Target Node
x : integer;
y : integer;
vecNeighBour : array of PNode;
Parent : PNode;
end;
OnFormShow I fill the array :
SetLength(Node,4,4);
Image1.Height:=Length(Node)*50;
Image1.Width:=Length(Node)*50;
for x := Low(Node) to High(Node) do
for y := Low(Node) to High(Node) do
begin
Node[x,y].Obstacle:=false;
Node[x,y].Visited:=false;
Node[x,y].GCost:=infinity;
Node[x,y].HCost:=infinity;
Node[x,y].x:=x;
Node[x,y].y:=y;
end;
Now I would like to Sort this array by HCost , so I tried this .
TArray.Sort<TNode>(Node , TComparer<TNode>.Construct(
function(const Left, Right: TNode): double
begin
if Left.HCost > Right.HCost then
Result:=Left.HCost
else
Result:=Right.HCost;
end));
My knowledge is seriously lacking in this thing . I get a error from Delphi
"Incompatible types: 'System.Gerenics.Defaults.TComparison and 'Procedure'
What am I doing wrong here?
A comparison function has to return an Integer
value. It is defined like so:
type
TComparison<T> = reference to function(const Left, Right: T): Integer;
Your function returns the wrong type.
function CompareDoubleInc(Item1, Item2: Double): Integer;
begin
if Item1=Item2 then begin
Result := 0;
end else if Item1<Item2 then begin
Result := -1
end else begin
Result := 1;
end;
end;
....
TArray.Sort<TNode>(
Node,
TComparer<TNode>.Construct(
function(const Left, Right: TNode): Integer
begin
Result := CompareDoubleInc(Left.HCost, Right.HCost);
end
)
);
Or if you want to make it even simpler you can delegate to the default double comparer:
TArray.Sort<TNode>(
Node,
TComparer<TNode>.Construct(
function(const Left, Right: TNode): Integer
begin
Result := TComparer<Double>.Default.Compare(Left.HCost, Right.HCost);
end
)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With