[solved]sorting array of path pointers by distance in 3D model lazarus / 62-General ---------------------------------------------------------------------- tygraphics · 2024-08-09 16:19 Hi, I am working on a 3D vector application project using Free Pascal, and I need to sort an array of pointers referencing Path records. Each Path record contains a field that stores the distance from a camera to the center point of that path. The goal is to sort this array of pointers in descending order based on the distance values so that during the rendering process, paths that are furthest away from the camera are drawn first, and those closest are drawn last. I appreciate any help you can provide. Thomas Young ---------------------------------------------------------------------- Hartmut · 2024-08-09 16:55 can you show as the declaration of the records and the array of pointers? And how many data (about) do you want to sort? ---------------------------------------------------------------------- tooldeveloper · 2024-08-09 18:26 The TFPObjectList class in the Contnrs unit may solve your issue.  Assuming I am reading your question correctly then below is a quick example of doing what I think you are asking. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  Contnrs; { TForm1 }   type    T3DVector = record      CamDistance : Int64;    end;    P3DVector = ^T3DVector; function VectorSort( Item1 : pointer; Item2: Pointer ) : Integer;  var    Vec1 : P3DVector;    Vec2 : P3DVector;  begin    // <0 Item1 is closer, 0=Same Distance, >0 Item2 is closer    Vec1 := Item1;    Vec2 := Item2;     result := (Vec2^.CamDistance - Vec1^.CamDistance);  end; procedure TForm1.Button1Click(Sender: TObject);  var    fpo : TFPObjectList;    idx : integer;    Item1 : T3DVector;    Item2 : T3DVector;    Item3 : T3DVector;    Item4 : T3DVector;  begin    fpo := TFPObjectList.Create;    fpo.OwnsObjects := False;     Item1.CamDistance := 111;    Item2.CamDistance := 222;    Item3.CamDistance := 333;    Item4.CamDistance := 444;     fpo.Add(TObject(@Item1));    fpo.Add(TObject(@Item2));    fpo.Add(TObject(@Item3));    fpo.Add(TObject(@Item4));     Memo1.Lines.Clear;    Memo1.Lines.Add( 'Pre-Sort' );    for idx := 0 to fpo.Count-1 do      begin        Memo1.Lines.Add( Format('Items[%d]=%d', [idx, P3DVector(fpo.Items[idx])^.CamDistance]));      end;     Memo1.Lines.Add( '' );    Memo1.Lines.Add( 'Sorting the vectors' );  fpo.Sort(@VectorSort);     Memo1.Lines.Add( '' );    Memo1.Lines.Add( 'Post-Sort' );    for idx := 0 to fpo.Count-1 do      begin        Memo1.Lines.Add( Format('Items[%d]=%d', [idx, P3DVector(fpo.Items[idx])^.CamDistance]));      end;     fpo.Free;  end;  The resulting output is: Code: [Select]Pre-Sort Items[0]=111 Items[1]=222 Items[2]=333 Items[3]=444 Sorting the vectors Post-Sort Items[0]=444 Items[1]=333 Items[2]=222 Items[3]=111 Explanation: The TFPObjectList is storing the pointers to all of the items, in the above case they are simply pointers to simple records existing on the stack. When I call the "Sort" method on the TFPObjectList object I'm supplying the custom VectorSort comparison function that compares the CamDistance for each of the two objects. In a default Sort function it would "subtract" the second item from the first item. In the above example, the VectorSort function is subtracting the first item from the second to achieve the reverse sort. ---------------------------------------------------------------------- bytebites · 2024-08-09 20:24 Use code tags.