我做的一个程序里需要对列表中的多个列进行排序.从网上下载的酸法都是只能排一列,我想是不是应该在原有算法的比较部分做一下改动,应该可以适应多列排序,但我怎么弄也不行.急请各位高手帮忙!!我用的QuickSort,  procedure QSort(iLo,iHi:Integer);
  var
    Lo, Hi, Mid : Integer;
  begin
    Lo := iLo;
    Hi := iHi;
    Mid := (Lo + Hi) div 2;
    repeat
      while Compare(Self, Items[Lo],Items[Mid],gt) do Inc(Lo);
      while Compare(Self, Items[Hi],Items[Mid],lt) do Dec(Hi);
      if Lo <= Hi then
      begin
        exchange(Lo,Hi);
        Inc(Lo);
        Dec(Hi);
      end;
    until Lo > Hi;
    if Hi > iLo then QSort(iLo, Hi);
    if Lo < iHi then QSort(Lo, iHi);
  end;其中:
  Compare是回调函数,声明如下:
  
  TCompareFunction = function (Sender:TCustomSort; const AItemA,AItemB : Pointer;
    const AShouldBe : TCompare) : Boolean of Object;

解决方案 »

  1.   

    TCompare = (lt, gt, eq, ltOeq);
      

  2.   

    lt是>;gt是<;eq是=;ltoeq是<>
      

  3.   

    //这是TMS Grids中单列排序算法,你可以下来看看
    procedure TAdvStringGrid.SortByColumn(Col: Integer);
    var
      Idx: Word;
      Changed: Boolean;
    begin
      if RowCount < 2 then
        Exit;  if FSortSettings.Direction = sdAscending then
        SortDir := 1
      else
        SortDir := -1;  repeat
        Changed := False;
        for Idx := FixedRows to RowCount - 2 - FixedFooters do
        begin
          if SortDir = Compare(Col,Idx,Idx + 1) then /
          begin
            SortSwapRows(Idx,Idx + 1);
            Changed := True;
          end;
        end;
      until Changed = False;
    end;
      

  4.   

    自己写个比较大小的函数代替"<",">"就可以了
      

  5.   

    function TMemDataSet.CompareItem(Sender: TCustomSort; const AItemA,
      AItemB: Pointer): Integer;
    var
      I : integer;
      ACol: Integer;
      Value1, Value2:Variant;
    begin
      Result := 0;
      
      for I := 0 to Sender.CompareFields.ItemCount - 1 do
      begin
        {取得比较的列的序号}
        ACol:=Sender.CompareFields.Items[i].FieldIndex;
        {取得两条记录的数值}
        Value1:=TMemRecord(AItemA).FieldValues[ACol];
        Value2:=TMemRecord(AItemB).FieldValues[ACol];
        {两条记录比较}
        case Sender.CompareFields.Items[I].FieldType of
          mftInteger:begin
            Result := CompareInteger(Value1, Value2);
          end;
          mftFloat:begin
            Result := CompareFloat(Value1, Value2);
          end;
          mftString:begin
            Result := AnsiCompareText(Value1, Value2);
          end;
          mftBoolean:begin
            Result := CompareBoolean(Value1, Value2);
          end;
        end;    {如果降序排列,则Flag取反}
        if Sender.CompareFields.Items[I].SortOrder = csDesc then
          Result := - Result;    {如果不是等于,则退出比较}
        if Result <> 0 then Break;
      end;
    end;