怎么才能实现像资源管理器那样的排序功能,能够倒序和顺序!!

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ComCtrls;type
      TForm1 = class(TForm)
        ListView1: TListView;
        procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
        procedure ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
          Data: Integer; var Compare: Integer);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      ColumnToSort: Integer;
      IsAscSort: Boolean;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      ColumnToSort := -1;
      IsAscSort := True;
    end;procedure TForm1.ListView1ColumnClick(Sender: TObject;
      Column: TListColumn);
    begin
      if ColumnToSort <> Column.Index then
        IsAscSort := True
      else
        IsAscSort := not IsAscSort;
      ColumnToSort := Column.Index;
      (Sender as TCustomListView).AlphaSort;
    end;procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
      Data: Integer; var Compare: Integer);
    var
      ix: Integer;
    begin
      if ColumnToSort = 0 then
        if IsAscSort then
          Compare := CompareText(Item1.Caption, Item2.Caption)
        else
          Compare := CompareText(Item2.Caption, Item1.Caption)
      else begin
        ix := ColumnToSort - 1;
        if IsAscSort then
          Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix])
        else
          Compare := CompareText(Item2.SubItems[ix],Item1.SubItems[ix]);
      end;
    end;end.
      

  2.   

    function ListViewRowSort(mListView: TListView; mByte: integer; mDesc: Boolean =
      False): Boolean;
      function ListViewRowSwap(mListView: TListView; mFromRow, mToRow: Integer):
          Boolean;
      var
        S: string;
      begin
        Result := False;
        if (mToRow = mFromRow) then
          Exit;
        if not Assigned(mListView) then
          Exit;
        if (mFromRow < 0) or (mFromRow >= mListView.Items.Count) then
          Exit;
        if (mToRow < 0) or (mToRow >= mListView.Items.Count) then
          Exit;
        try
          S := mListView.Items.Item[mFromRow].Caption;
          mListView.Items.Item[mFromRow].Caption :=
            mListView.Items.Item[mToRow].Caption;
          mListView.Items.Item[mToRow].Caption := S;
        except
          Exit;
        end;
        Result := True;
      end;
    var
      I, J: Integer;
    begin
      Result := False;
      if not Assigned(mListView) then
        Exit;
      for I := 0 to mListView.Items.Count - 2 do
        for J := I + 1 to mListView.Items.Count - 1 do
          if mDesc then
            if RightStr(mListView.Items.Item[I].Caption, mByte) <
              RightStr(mListView.Items.Item[J].Caption, mByte) then
              ListViewRowSwap(mListView, I, J)
            else
          else if RightStr(mListView.Items.Item[I].Caption, mByte) >
            RightStr(mListView.Items.Item[J].Caption, mByte) then
            ListViewRowSwap(mListView, I, J);
      Result := True;
    end;
    //参数说明?:ListView,从第几字节开始排序,升序or降序
      

  3.   

    回一楼的:
        十分感谢你的代码,我刚刚COPY去用了下,但有一个地方该改动一下:procedure TForm1.FormCreate(Sender: TObject);
    begin
      ColumnToSort := -1;//此处的 (-1) 改为 (1)
      IsAscSort := True;
    end;如果不改,就没法给例表动态添加项目了,如果用
      with listview.add do Caption:='Test';
    两次就会出现一个错误:
     EStringListError,List index out of bounds.(-2)
    你这个错误还真的难找,找了我一个下午。http://expert.csdn.net/Expert/TopicView1.asp?id=1891201