How to do sorting in StringGrid?

I have a StringGrid table like this:

table

How do I make sure that when you click the "sort by group" and "sort by last name" buttons, for example, the data is sorted from A to Z by the corresponding columns?

Author: Kromster, 2016-05-23

1 answers

If we use the simplest bubble sorting:

procedure TfmMain.SortGrid(Column: integer);
var
  i, j: integer;
  tmpRow: TStringList;
begin
  tmpRow:= TStringList.Create;
  try
    for i:=0 to StringGrid.RowCount-1 do
      for j:=i+1 to StringGrid.RowCount-1 do
        // сортируем по возрастанию.
        if AnsiCompareStr(StringGrid.Cells[Column, i], StringGrid.Cells[Column, j])>0 then
          begin
            tmpRow.Assign(StringGrid.Rows[i]);
            StringGrid.Rows[i]:=StringGrid.Rows[j];
            StringGrid.Rows[j]:=tmpRow;
          end;
  finally
    tmpRow.Free;
  end;
end;
 1
Author: kami, 2016-05-23 08:18:40