Author: Tomas Rutkauskas
Does anyone know an easy way to sort TStringGrids, preferably by the first column?
Answer:
Here is a quick Bubblesort:
1 procedure TForm1.Button1Click(Sender: TObject);
2 var3 pass, j: integer;
4 hold: TStringList;
5 begin6 hold := TStringList.Create;
7 {sorting is based on first column, '0'}8 for pass := 1 to StringGrid1.RowCount - 1 do9 begin10 for j := 0 to StringGrid1.RowCount - 2 do11 if StringGrid1.Cells[0, j] > StringGrid1.Cells[0, j + 1] then12 begin13 hold.Assign(StringGrid1.Rows[j]);
14 StringGrid1.Rows[j].Assign(StringGrid1.Rows[j + 1]);
15 StringGrid1.Rows[j + 1].Assign(hold);
16 end;
17 end;
18 hold.Free;
19 end;