Function Compare(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
begin
  Result := lstrcmp(PChar(TListItem(Item1).Caption),
                     PChar(TListItem(Item2).Caption));end;
procedure TForm1.ListView1ColumnClick(Sender: TObject;
  Column: TListColumn);
begin
  ListView1.CustomSort(@Compare,0);
end;

解决方案 »

  1.   

    还可以试试这个方法:
    type
        PSortInfo = ^TSortInfo;
        TSortInfo = record
            Col     : Integer;
            Style   : TSortStyle;
            Asc     : Boolean;
        end;function  ListViewCompare(I1, I2: TListItem; Data: Integer): Integer; stdcall;
    var
        V1, V2: string;    function    Sign(Val: Extended): Integer;
        begin
            if Val < 0 then
                Result := -1
            else if Val > 0 then
                Result := 1
            else
                Result := 0;
        end;    function    ExtractNum(const S: string): string;
        var
            i, j: Integer;
        begin
            j := 0;
            for i := 1 to Length(S) do
                if S[i] in ['0'..'9'] then
                    Inc(j)
                else
                    Break;
            if j = 0 then
                Result := '0'
            else
                Result := Copy(S,1,j);
        end;
    begin
        with PSortInfo(Data)^ do
        begin        if Col = 0 then
            begin
                V1 := I1.Caption;
                V2 := I2.Caption;
            end
            else
            begin
                V1 := I1.SubItems[Col-1];
                V2 := I2.SubItems[Col-1];
            end;        case Style of
                ssAlpha     : Result := AnsiCompareText(V1,V2);
                ssNumeric   : Result := Sign(StrToFloat(ExtractNum(V1))-StrToFloat(ExtractNum(V2)));
                ssDateTime  : Result := Sign(StrToDateTime(V1) - StrToDateTime(V2));
            else
                Result := 0;
            end;        if not Asc then
                Result := -Result;
        end;
    end;procedure SortListView(ListView:TListView; ColumnIndex:Integer;
                           Style: TSortStyle; Ascending: Boolean=True);
    {排序ListView,ColumnIndex:排序列索引号,
     Style 排序方式:按字符,按数值,按日期(日期格式为
     SysUtils.ShortDataTimeFmt,缺省为YY-MM-DD);
     Ascending:=True按升序,否则按降序}
    var
      FSortInfo:TSortInfo;
    begin
        FSortInfo.Col   := ColumnIndex;
        FSortInfo.Style := Style;
        FSortInfo.Asc   := Ascending;
        ListView.CustomSort(@ListViewCompare,LongInt(@FSortInfo));
    end;