帮我写一段使用delphi对stringgrid string进行按照字母顺序正反序排序
时间: 2023-11-12 07:08:01 浏览: 35
好的,这个问题属于技术类问题,可以回答。以下是使用 Delphi 对 StringGrid 中的字符串进行正序和反序排序的示例代码:
// 正序排序
procedure SortStringGrid(SG: TStringGrid; ColIndex: Integer);
var
I, J: Integer;
Temp: string;
begin
for I := 1 to SG.RowCount - 1 do
for J := I + 1 to SG.RowCount - 1 do
if CompareStr(SG.Cells[ColIndex, I], SG.Cells[ColIndex, J]) > 0 then
begin
Temp := SG.Rows[J].CommaText;
SG.Rows[J].CommaText := SG.Rows[I].CommaText;
SG.Rows[I].CommaText := Temp;
end;
end;
// 反序排序
procedure SortStringGridReverse(SG: TStringGrid; ColIndex: Integer);
var
I, J: Integer;
Temp: string;
begin
for I := 1 to SG.RowCount - 1 do
for J := I + 1 to SG.RowCount - 1 do
if CompareStr(SG.Cells[ColIndex, I], SG.Cells[ColIndex, J]) < 0 then
begin
Temp := SG.Rows[J].CommaText;
SG.Rows[J].CommaText := SG.Rows[I].CommaText;
SG.Rows[I].CommaText := Temp;
end;
end;
使用方法:
// 正序排序
SortStringGrid(StringGrid1, 1);
// 反序排序
SortStringGridReverse(StringGrid1, 1);
其中,StringGrid1 为要排序的 StringGrid,ColIndex 为要排序的列索引。