[Solved] Sort TStringgrid, exclude empty cells but keep Grid.Objects lazarus / 21-General ---------------------------------------------------------------------- Hansvb · 2025-04-11 13:49 Hi, I would like to sort a TStringGrid. The empty cells should be ignored during sorting. This works with the following function: 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 function TfrmMain.SortGridByColumn(Grid : TStringGrid; Column : Integer;  Ascending : Boolean) : Boolean;var  i, j: Integer;  Temp: string;begin  Result := True;  try    for i := 0 to Grid.RowCount - 2 do      for j := i + 1 to Grid.RowCount - 1 do        if (Grid.Cells[Column, j] <> '') and ((Grid.Cells[Column, i] = '') or (CompareText(Grid.Cells[Column, i], Grid.Cells[Column, j]) > 0 = Ascending)) then        begin          Temp := Grid.Cells[Column, i];          Grid.Cells[Column, i] := Grid.Cells[Column, j];          Grid.Cells[Column, j] := Temp;        end;  except    Result := False;  end;end;   I also have StringGrid.Objects[] They should be assigned to the correct cell again. See the function below, This crashes with an access violation. I am probably not cleaning up my object in the right place? What am I missing? 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 function TfrmMain.SortGridByColumn(Grid : TStringGrid; Column : Integer;  Ascending : Boolean) : Boolean;var  i, j: Integer;  Temp: string;  DataObject: P_Item_ObjectData;begin  Result := True;  try    for i := 0 to Grid.RowCount - 2 do      for j := i + 1 to Grid.RowCount - 1 do        if (Grid.Cells[Column, j] <> '') and ((Grid.Cells[Column, i] = '') or (CompareText(Grid.Cells[Column, i], Grid.Cells[Column, j]) > 0 = Ascending)) then        begin          Temp := Grid.Cells[Column, i];          DataObject := P_Item_ObjectData(Grid.Objects[Column, j]); //           if P_Item_ObjectData(Grid.Objects[Column, j]) <> nil then begin//            Dispose(P_Item_ObjectData(Grid.Objects[Column, j]));  // access violation in row 5 ?//            Grid.Objects[Column, j] := Nil;          end;           if DataObject <> nil then begin            if P_Item_ObjectData(Grid.Objects[Column, j]) <> nil then begin              Dispose(P_Item_ObjectData(Grid.Objects[Column, j]));  // access violation in row 5 ?              Grid.Objects[Column, j] := Nil;          end;             Grid.Objects[Column, j] := TObject(DataObject);          end;           Grid.Cells[Column, i] := Grid.Cells[Column, j];          Grid.Cells[Column, j] := Temp;       end;  except    Result := False;  end;end; Or is there a standard sorting function in TSTringgrid which ignores empty cells an keeps track of Grid.Objects? ---------------------------------------------------------------------- cdbc · 2025-04-11 14:05 Hi In your first algorithm, I would probably make 'Temp' a 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 type  TCopyRec = record    crObj: TObject;    crStr: String;  end; and then: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 function TfrmMain.SortGridByColumn(Grid : TStringGrid; Column : Integer;  Ascending : Boolean) : Boolean;var  i, j: Integer;  Temp: TCopyRec;begin  Result := True;  try    for i := 0 to Grid.RowCount - 2 do      for j := i + 1 to Grid.RowCount - 1 do        if (Grid.Cells[Column, j] <> '') and ((Grid.Cells[Column, i] = '') or (CompareText(Grid.Cells[Column, i], Grid.Cells[Column, j]) > 0 = Ascending)) then        begin          Temp.crStr := Grid.Cells[Column, i];          Temp.crObj := Grid.Objects[Column, i];          Grid.Cells[Column, i] := Grid.Cells[Column, j];          Grid.Objects[Column, i] := Grid.Objects[Column, j];          Grid.Cells[Column, j] := Temp.crStr;          Grid.Objects[Column, j] := Temp.crObj;        end;  except    Result := False;  end;end;  Regards Benny ---------------------------------------------------------------------- Hansvb · 2025-04-12 07:03 Thanks, that works. There was another thinking error in the sort routine that I hadn't seen. I lost 1 value (cell content) of my stringgrid. That has now been adjusted as well. This is the new basis that I can continue with. (Leaving the record aside for now, it will probably go to an ExecNoRes if you know what I mean ). 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 function TfrmMain.SortGridByColumn(Grid: TStringGrid; Column: Integer;  Ascending: Boolean): Boolean;var  i, j: Integer;  TempStr: string;  TempObj: TObject;  StartRow: Integer;begin  Result := True;  try    StartRow := Grid.FixedRows; // Do not sort the header(s), skip them.     for i := StartRow to Grid.RowCount - 2 do      for j := i + 1 to Grid.RowCount - 1 do      begin        if (Grid.Cells[Column, j] <> '') then        begin          if (Grid.Cells[Column, i] = '') or           (Ascending and (CompareText(Grid.Cells[Column, i], Grid.Cells[Column, j]) > 0)) or             (not Ascending and (CompareText(Grid.Cells[Column, i], Grid.Cells[Column, j]) < 0)) then  //  only Ascending for now as test.          begin            // Switch the cell contents includinng the "grid.objects".            TempStr := Grid.Cells[Column, i];            TempObj := Grid.Objects[Column, i];             Grid.Cells[Column, i] := Grid.Cells[Column, j];            Grid.Objects[Column, i] := Grid.Objects[Column, j];             Grid.Cells[Column, j] := TempStr;            Grid.Objects[Column, j] := TempObj;          end;        end;      end;  except    Result := False;  end;end; ---------------------------------------------------------------------- cdbc · 2025-04-12 08:01 Hi Hans Glad to hear it works  Yup - I know  Regards Benny ---------------------------------------------------------------------- Nicole · 2025-04-12 14:07 Not sure, if there is a setting for ignoring empty lines there. However there exists a component names alike "Dynamic String Grid" of Tony, which allows the sorting by the click of the mouse on all columns as in Excel. ---------------------------------------------------------------------- wp · 2025-04-12 21:28 I am confused by the name of your procedure: "SortGridByColumn" implies for me that the entire grid is sorted so that the cells in the given column are ordered; I would expect that complete rows are exchanged so that each cell in each row keeps its horizontal neighbour. (This is already available in TStringGrid as method SortColRow(IsColumn: Boolean; index:Integer). Your code, however, sorts only the cells in the given column, all other cells are not touched. If this really is your intention you could do this alternatively by copying the column to be sorted into a TStringList which has sorting capabilities. You'll have to provide a custom compare function because you want to have blank cells at the end of the column after sorting. The attached project demonstrates all cases: - Button1 triggers your function "SortGridByColumn". - Button2 triggers TStringGrid.SortByColRow and sorts the entire grid - Button3 uses the auxiliary TStringList for sorting. All the non-blank cells in this demo grid have an object attached; it is the same integer shown as cell text cast to a pointer. When you click into the grid the Cells[] text and the Objects[] are read from the clicked cell and displayed by two labels - both labels should always display the same. Note that all sorting methods tested keep Cells[] and Objects[] together. In case of SortByColRow this is because the grid stores Cells[] and Objects[] as elements of the same internal record (TCellProps) and thus stay together automatically when the cell records are rearranged during sorting. In case of the aux stringlist, the Assign method also copies the pointers along with the texts into the destination list. ---------------------------------------------------------------------- Hansvb · 2025-04-13 07:32 Quote sorts only the cells in the given column, all other cells are not touched. If this really is your intention you could do this alternatively by copying the column to be sorted into a TStringList It is indeed my intention to sort only 1 column. I had not thought of possibly copying it to a TStringlist. I will save your example, if I still want to sort all rows and exclude the empty cells, I can use that well.