Sort a TStringGrid using the Bubblesort algorithm (Views: 29)
Problem/Question/Abstract: Does anyone know an easy way to sort TStringGrids, preferably by the first column? Answer: Here is a quick Bubblesort: procedure TForm1.Button1Click(Sender: TObject); var pass, j: integer; hold: TStringList; begin hold := TStringList.Create; {sorting is based on first column, '0'} for pass := 1 to StringGrid1.RowCount - 1 do begin for j := 0 to StringGrid1.RowCount - 2 do if StringGrid1.Cells[0, j] > StringGrid1.Cells[0, j + 1] then begin hold.Assign(StringGrid1.Rows[j]); StringGrid1.Rows[j].Assign(StringGrid1.Rows[j + 1]); StringGrid1.Rows[j + 1].Assign(hold); end; end; hold.Free; end; |