nielsAD / lape

Scripting engine with Pascal-like syntax for FPC and Delphi
119 stars 28 forks source link

Add SortWeighted #167

Closed ollydev closed 3 years ago

ollydev commented 3 years ago

Sort lacks the ability to provide any data to the compare callback. So i've added SortWeighted. Example:

function dist(fromX, fromY, toX, toY: Integer): Extended;
begin
  Result := Hypot(fromX-toX, fromY-toY);
end;

var
  points: array of record x,y: Integer; end;
  weights: array of Extended;
  i: Integer;
begin
  points += [200,200];
  points += [100,100];
  points += [150,150];
  points += [75, 75];
  points += [80, 80];

  for i:=0 to High(points) do
    weights += dist(points[i].x, points[i].y, 100, 100);

  SortWeighted(points, weights); // points are now sorted from 100,100