用listview显示学生基本信息,如下:
学号   姓名   ……
4      aaa    ……
1      ddd    ……
16     qqq    ……
5      uuu    ……
等等怎么让他们根据学号排序。注意,数据不是从数据库里查出来的,所以不能用order by。
我查了帮助,要写一个方法:
function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
begin
  Result := -CompareText(Item1.Caption,Item2.Caption);
end;然后再写:
procedure TForm1.Button1Click(Sender: TObject);
begin
    ListView1.CustomSort(@CustomSortProc, 0);
end;我还是不明白,谁能帮我具体化一下???
谢谢了~

解决方案 »

  1.   

    用冒泡排序法比较好,你可以参考一下数据结构那本书,有pascal版和c版
      

  2.   

    delphi里有现成的方法,我为什么要去“冒泡”!!!
      

  3.   

    要按字符串排序可以用TCustomListView.Alphasort
    procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);begin
      ColumnToSort := Column.Index;
      (Sender as TCustomListView).AlphaSort;
    end;
      

  4.   

    你用的方法是Tlistview提供的排序方法。他发送自定义消息LVM_SORTITEMS给listview控件,实现排序。
    CustomSort的两个参数。
    SortProc: TTVCompare 具体的排序函数。返回值是被比较的两个Item之间的关系
    Return Value Meaning
    < 0 IParam1 comes before IParam2
    0 IParam1 and IParam2 are equivalent
    > 0 IParam2 comes before IParam1例子中加负号,是指逆排序
    如果你想使你的列表的 第二列正排序,只需要修改 CustomSortProc
    Result := CompareText(Item1.subitems.strings[0],Item2.subitems.strings[0]);就可以了。