procedure TFrmDBDL.GridQuickSort(Grid: TStringGrid; ACol: Integer; Order,
NumOrStr: Boolean);
( * 函数功能:给 StringGrid 的 ACol 列快速法排序
( * Order: True 从小到大
( * : False 从大到小
( * NumOrStr : true 值的类型是Integer
( * : False 值的类型是String
( * 函数说明:对于日期,时间等类型数据均可按字符方式排序, * )
procedure MoveStringGridData(Grid: TStringGrid; Sou,Des :Integer );
var
TmpStrList: TStringList ;
K : Integer ;
begin
TmpStrList : = TStringList.Create() ;
try
TmpStrList : = TStringList.Create() ;
TmpStrList.Clear ;
for K : = Grid.FixedCols to Grid.ColCount - 1 do
TmpStrList.Add(Grid.Cells[K,Sou]) ;
Grid.Rows [Sou] : = Grid.Rows [Des] ;
for K : = Grid.FixedCols to Grid.ColCount - 1 do
Grid.Cells [K,Des]: = TmpStrList.Strings[K] ;
finally
TmpStrList.Free ;
end;
end;
procedure QuickSort(Grid: TStringGrid; iLo, iHi: Integer);
var
Lo, Hi : Integer;
Mid: String ;
begin
Lo : = iLo ;
Hi : = iHi ;
Mid : = Grid.Cells[ACol,(Lo + Hi) div 2 ];
repeat
if Order and not NumOrStr then // 按正序、字符排
begin
while Grid.Cells[ACol,Lo] < Mid do Inc(Lo);
while Grid.Cells[ACol,Hi] > Mid do Dec(Hi);
end ;
if not Order and not NumOrStr then // 按反序、字符排
begin
while Grid.Cells[ACol,Lo] > Mid do Inc(Lo);
while Grid.Cells[ACol,Hi] < Mid do Dec(Hi);
end;
if NumOrStr then
begin
if Grid.Cells[ACol,Lo] = '' then Grid.Cells[ACol,Lo] : = ' 0 ' ;
if Grid.Cells[ACol,Hi] = '' then Grid.Cells[ACol,Hi] : = ' 0 ' ;
if Mid = '' then Mid : = ' 0 ' ;
if Order then
begin // 按正序、数字排
while StrToFloat(Grid.Cells[ACol,Lo]) < StrToFloat(Mid) do Inc(Lo);
while StrToFloat(Grid.Cells[ACol,Hi]) > StrToFloat(Mid) do Dec(Hi);
end else
begin // 按反序、数字排
while StrToFloat(Grid.Cells[ACol,Lo]) > StrToFloat(Mid) do Inc(Lo);
while StrToFloat(Grid.Cells[ACol,Hi]) < StrToFloat(Mid) do Dec(Hi);
end;
end ;
if Lo <= Hi then
begin
MoveStringGridData(Grid, Lo, Hi) ;
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSort(Grid, iLo, Hi);
if Lo < iHi then QuickSort(Grid, Lo, iHi);
end;
begin
try
QuickSort(Grid, Grid.FixedRows, Grid.RowCount - 1 ) ;
except
on E: Exception do
Application.MessageBox(Pchar( ' 系统在排序数据的时候遇到异常: ' # 13 + E.message + # 13 ' 请重试,如果该问题依然存在请与程序供应商联系! ' ), ' 系统错误 ' ,MB_OK + MB_ICONERROR) ;
end;
end;
NumOrStr: Boolean);
( * 函数功能:给 StringGrid 的 ACol 列快速法排序
( * Order: True 从小到大
( * : False 从大到小
( * NumOrStr : true 值的类型是Integer
( * : False 值的类型是String
( * 函数说明:对于日期,时间等类型数据均可按字符方式排序, * )
procedure MoveStringGridData(Grid: TStringGrid; Sou,Des :Integer );
var
TmpStrList: TStringList ;
K : Integer ;
begin
TmpStrList : = TStringList.Create() ;
try
TmpStrList : = TStringList.Create() ;
TmpStrList.Clear ;
for K : = Grid.FixedCols to Grid.ColCount - 1 do
TmpStrList.Add(Grid.Cells[K,Sou]) ;
Grid.Rows [Sou] : = Grid.Rows [Des] ;
for K : = Grid.FixedCols to Grid.ColCount - 1 do
Grid.Cells [K,Des]: = TmpStrList.Strings[K] ;
finally
TmpStrList.Free ;
end;
end;
procedure QuickSort(Grid: TStringGrid; iLo, iHi: Integer);
var
Lo, Hi : Integer;
Mid: String ;
begin
Lo : = iLo ;
Hi : = iHi ;
Mid : = Grid.Cells[ACol,(Lo + Hi) div 2 ];
repeat
if Order and not NumOrStr then // 按正序、字符排
begin
while Grid.Cells[ACol,Lo] < Mid do Inc(Lo);
while Grid.Cells[ACol,Hi] > Mid do Dec(Hi);
end ;
if not Order and not NumOrStr then // 按反序、字符排
begin
while Grid.Cells[ACol,Lo] > Mid do Inc(Lo);
while Grid.Cells[ACol,Hi] < Mid do Dec(Hi);
end;
if NumOrStr then
begin
if Grid.Cells[ACol,Lo] = '' then Grid.Cells[ACol,Lo] : = ' 0 ' ;
if Grid.Cells[ACol,Hi] = '' then Grid.Cells[ACol,Hi] : = ' 0 ' ;
if Mid = '' then Mid : = ' 0 ' ;
if Order then
begin // 按正序、数字排
while StrToFloat(Grid.Cells[ACol,Lo]) < StrToFloat(Mid) do Inc(Lo);
while StrToFloat(Grid.Cells[ACol,Hi]) > StrToFloat(Mid) do Dec(Hi);
end else
begin // 按反序、数字排
while StrToFloat(Grid.Cells[ACol,Lo]) > StrToFloat(Mid) do Inc(Lo);
while StrToFloat(Grid.Cells[ACol,Hi]) < StrToFloat(Mid) do Dec(Hi);
end;
end ;
if Lo <= Hi then
begin
MoveStringGridData(Grid, Lo, Hi) ;
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSort(Grid, iLo, Hi);
if Lo < iHi then QuickSort(Grid, Lo, iHi);
end;
begin
try
QuickSort(Grid, Grid.FixedRows, Grid.RowCount - 1 ) ;
except
on E: Exception do
Application.MessageBox(Pchar( ' 系统在排序数据的时候遇到异常: ' # 13 + E.message + # 13 ' 请重试,如果该问题依然存在请与程序供应商联系! ' ), ' 系统错误 ' ,MB_OK + MB_ICONERROR) ;
end;
end;