I have this StringGrid table:

table

How to make, that when pressing buttons "sorting by group" and "sorting by last name", for example, the data are sorted from AZ by the corresponding columns?

    1 answer 1

    If you use the simplest bubble sort:

    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; 
    • In the Button wrote the following text: SgSort (StringGrid1, 1); It gives an error: 'List index out of bounds (28)' - Nick
    • SgSort This is great, but where do you see the SgSort method in my code? Even if it is just a change in the name of the method I proposed, the parameter list is different. Conclusion - you have adapted the code presented in the answer, to fit your needs. This is commendable, but I cannot know what exactly you have changed, so I cannot say where you have an error in the code either. List index out of bounds in the code I give cannot occur - all data is selected within the valid range for StringGrid - kami