TMSFMXGrid group sorting - possible fix

The sorting of grouped rows was not working correctly and I saw that another user posted the same issue over two years ago.  Not sure if my code is a hacked up way to fix the sorting issues but at least it appears to do the job.  So, if you have the source code, give it a try.  I did away with their hiddenrow stuff, so not sure of the full impact of that, but for how I use the grid, it's not an issue.


procedure TTMSFMXGridData.SortGroupedInt(Indexed: boolean);
var
  i,rr,r1,r2,span,idx: integer;
  nodeWasClosed: array of boolean;
begin
  { Save Open/Closed states of all nodes }
  i := FixedRows;
  while  i <= (FRowCount - FixedFooterRows) do
  begin
    rr := DisplToRealRow(i);
    if IsNode(rr) then
    begin
      idx := Length(nodeWasClosed);
      SetLength(nodeWasClosed, idx + 1);
      if GetNodeState(rr) = nsClosed then
        nodeWasClosed[idx] := True
      else
        nodeWasClosed[idx] := False;
    end;
    inc(i);
  end;

  { Open all Nodes so they can be sorted }
  OpenAllNodes;

  { Sort the nodes }
  BeginUpdate;
  try
    RowCount := RowCount + 1;  // pivot row
    i := FixedRows;
    while  i <= (FRowCount - FixedFooterRows - 3) do
    begin
      rr := DisplToRealRow(i);
      if IsNode(rr) then
      begin
        span := GetNodeSpan(rr);
        r1 := rr + 1;
        r2 := rr + span;

        if r2 - r1 >= 1 then
        begin
          if Indexed then
            QuickSortRowsIndexed(0,r1,r2)
          else
            QuickSortRows(FSortColumn,r1,r2);
        end;

        i := i + span;
      end
      else
        inc(i);
    end;

    ClearCells(CellRange(0,RowCount - 1, ColumnCount - 1, RowCount - 1));
    RowCount := RowCount - 1; // Remove pivot row

    { Close any previously closed nodes }
    i := FixedRows;
    idx := 0;
    while  i <= (FRowCount - FixedFooterRows) do
    begin
      rr := DisplToRealRow(i);
      if IsNode(rr) then
      begin
        if nodeWasClosed[idx] then
          CloseNode(rr);
        inc(idx);
      end;
      inc(i);
    end;
  finally
    EndUpdate;
  end;

  UpdateGridCells;
end;


There was indeed an issue with collapsed groups. This technique is a convenient solution.

Next update will have this improvement.