Softtech
asked on
Sorting a TStringGrid
What is the best/easiest way to sort the contents of a TStringGrid? I have a TStringGrid which has 100 rows and 5 columns. I want the rows to be sorted based upon the contents of column #1.
Thanks.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Do something like this:
i,j.minimuRow : LongInt;
tempRow : TStrings;
for i:=0 to RowCount-2 do
begin
// sg - your StringGrid
// 1 for column 1
currentValue:=sg.Cells(i,1
// supose the min row
minimumRow:=i;
// search till rest for real min row
for j:=i+1 to RowCount - 1 do
if sg.Cells(j,1)<sg.Cells(min
minimumRow:=j;
// swap if some other row is min
if minimumRow<>i then
begin
tempRow:=sg.Rows[i];
sg.Rows[i]:=sg.Rows[minimu
sg.Rows[minimumRow]:=tempR
end;
end;