我在网上找到一段DELPHI LISTVIEW排序的代码,但实际使用时,一般数据都正常,但当列数据为时间时,就排序有问题,比如按时间升序排列,1:00:00,11:00:00,2:00:00,而不是按1:00:00,2:00:00,11:00:00,不知为何解决这个问题。
//用于控制升序及降序的转换
{var m_bSort:boolean;
function   CustomSortProc(Item1,   Item2:   TListItem;   ParamSort:   integer):   integer;   stdcall;
var   txt1,txt2   :   string;begin
    if   ParamSort   <>   0   then
    begin
            txt1   :=   Item1.SubItems.Strings[ParamSort   -   1];
            txt2   :=   Item2.SubItems.Strings[ParamSort   -   1];
            if   m_bSort   then
            begin
                  Result   :=   CompareText(txt1,txt2);
            end
            else
            begin
                  Result   :=   -CompareText(txt1,txt2);
            end;
    end
    else
    begin
            if   m_bSort   then
            begin
                  Result   :=   CompareText(Item1.Caption,Item2.Caption);
            end   else   begin
                 Result   :=  -CompareText(Item1.Caption,Item2.Caption);
            end;
    end;
end;procedure   TMainForm.ListView1ColumnClick(Sender:   TObject; Column:   TListColumn);
begin
    MainList.CustomSort(@CustomSortProc,   Column.Index);
    m_bSort   :=   not m_bSort;
end; 
同时也找到过以下代码,但不知怎么调用它实现功能,请大家帮帮忙。
var ColumnToSort: Integer;
procedure TMainForm.ListView1ColumnClick(Sender: TObject; Column: TListColumn);
begin
     ColumnToSort := Column.Index;
     (Sender as TCustomListView).AlphaSort;
end;
// 寫 OnCompare 事件 
procedure TMainForm.ListView1Compare(Sender: TObject; Item1, Item2: TListItem; Data: Integer; var Compare: Integer);
var
     ix: Integer;
begin
     if ColumnToSort = 0 then
     Compare := CompareText(Item1.Caption,Item2.Caption)
     else begin
     ix := ColumnToSort - 1;
     Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
     end;
end;

解决方案 »

  1.   

    时间格式的问题
    1:00:00和11:00:00在排序时是会被看成是同一组的,因为第一个数字都是"1",可以把 时间格式设置为hh:mm:ss看看
      

  2.   

    FormatDateTime('hh:mm:ss', DateTime)
    CompareText(Str1, Str2)
      

  3.   

    日期比较和字符串比较有区别的。
    比较日期时,直接将字符串格式的日期转成TDateTime类型,然后当数字比较
    比较字符串时,用CompareText
      

  4.   


    function CustomSortProc(Item1, Item2: TListItem; ParamSort: Integer): Integer; stdcall;
    var
        txt1, txt2: string;    tT1, tT2: TTime;
    begin
        //假设选中日期列时ParamSort为2那么    if ParamSort <> 0 then
        begin
            if ParamSort = 2 then
            begin
                txt1 := Item1.SubItems.Strings[ParamSort - 1];
                txt2 := Item2.SubItems.Strings[ParamSort - 1];            tT1 := StrToTime(txt1);
                tT2 := StrToTime(txt2);            if m_bSort then
                    Result := tT1 - tT2
                else
                    Result := -(tT1 - tT2);
            end
            else
            begin
                txt1 := Item1.SubItems.Strings[ParamSort - 1];
                txt2 := Item2.SubItems.Strings[ParamSort - 1];
                if m_bSort then
                    Result := CompareText(txt1, txt2)
                else
                    Result := -CompareText(txt1, txt2);
            end;
        end
        else
        begin
            if m_bSort then
                Result := CompareText(Item1.Caption, Item2.Caption)
            else
                Result := -CompareText(Item1.Caption, Item2.Caption);
        end;
    end;
      

  5.   

    比较日期的时候,用CompareValueResult := CompareValue( StrToDateTime(txt1), StrToDateTime(txt2) );if not m_bSort then
      Result := -Result;
      

  6.   

    转换日期格式试一下。用CompareValue 函数。