给 StringGrid 的 ACol 列快速法排序

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;
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值