以前的一段代码
procedure SortStringGrid(Grid: TStringGrid; ColID: Integer;
DataType: TGridDataType);
var
TmpOrder: Boolean;
TmpStrList: TStrings;
i: Integer;
{交换行}
procedure MoveStringGridData(Grid: TStringGrid; Sou, Des: Integer);
var
K: Integer;
begin
TmpStrList.Clear;
for K := 0 to Grid.ColCount - 1 do //Grid.FixedCols
TmpStrList.Add(Grid.Cells[K, Sou]);
Grid.Rows[Sou] := Grid.Rows[Des];
for K := 0 to Grid.ColCount - 1 do //Grid.FixedCols
Grid.Cells[K, Des] := TmpStrList.Strings[K];
end;
{快速排序}
procedure QuickSort(Grid: TStringGrid; pLowRow, pHighRow: Integer);
var
LowRow, HighRow: Integer;
MidValue: string;
begin
LowRow := pLowRow;
HighRow := pHighRow;
MidValue := Grid.Cells[ColID, (LowRow + HighRow) div 2];
repeat
case DataType of
gdtString, gdtDateTime:
if TmpOrder then //正序、字符
begin
while Grid.Cells[ColID, LowRow] < MidValue do
Inc(LowRow);
while Grid.Cells[ColID, HighRow] > MidValue do
Dec(HighRow);
end
else
begin //反序、字符
while Grid.Cells[ColID, LowRow] > MidValue do
Inc(LowRow);
while Grid.Cells[ColID, HighRow] < MidValue do
Dec(HighRow);
end;
gdtNumber:
begin
if Grid.Cells[ColID, LowRow] = '' then
Grid.Cells[ColID, LowRow] := '0';
if Grid.Cells[ColID, HighRow] = '' then
Grid.Cells[ColID, HighRow] := '0';
if MidValue = '' then
MidValue := '0';
if TmpOrder then
begin //正序、数字
while StrToFloat(Grid.Cells[ColID, LowRow]) < StrToFloat(MidValue) do
Inc(LowRow);
while StrToFloat(Grid.Cells[ColID, HighRow]) > StrToFloat(MidValue) do
Dec(HighRow);
end
else
begin //反序、数字
while StrToFloat(Grid.Cells[ColID, LowRow]) > StrToFloat(MidValue) do
Inc(LowRow);
while StrToFloat(Grid.Cells[ColID, HighRow]) < StrToFloat(MidValue) do
Dec(HighRow);
end;
end;
end;
if LowRow <= HighRow then
begin
MoveStringGridData(Grid, LowRow, HighRow);
Inc(LowRow);
Dec(HighRow);
end;
until LowRow > HighRow;
if HighRow > pLowRow then
QuickSort(Grid, pLowRow, HighRow);
if LowRow < pHighRow then
QuickSort(Grid, LowRow, pHighRow);
end;
begin
try
TmpStrList := TStringList.Create;
{决定顺序}
case DataType of
gdtString, gdtDateTime:
TmpOrder := Grid.Cells[ColID, Grid.FixedRows] > Grid.Cells[ColID,
Grid.FixedRows + 1];
gdtNumber:
for i := 0 to 254 do
begin
if StrToFloat(Grid.Cells[ColID, Grid.FixedRows + i]) =
StrToFloat(Grid.Cells[ColID, Grid.FixedRows + i + 1]) then
Continue
else
begin
TmpOrder := StrToFloat(Grid.Cells[ColID, Grid.FixedRows + i]) >
StrToFloat(Grid.Cells[ColID, Grid.FixedRows + i + 1]);
break;
end;
end;
end;
try
QuickSort(Grid, Grid.FixedRows, Grid.RowCount - 1);
except
on E: Exception do
raise;
end;
finally
TmpStrList.Free;
end;
end;