全局变量:
cancompare : Boolean;
sortstyle : Boolean;
ColumnToSort : Integer;
procedure Tmainform.ListView9Compare(Sender: TObject; Item1,
Item2: TListItem; Data: Integer; var Compare: Integer);
var
ix : Integer;
begin
if cancompare then exit;
if not SortStyle Then
if ColumnToSort = 0 then
Compare := CompareText(Item1.Caption,Item2.Caption)
else begin
ix := ColumnToSort - 1;
Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
end
Else
if ColumnToSort = 0 then
Compare := CompareText(Item2.Caption,Item1.Caption)
else begin
ix := ColumnToSort - 1;
Compare := CompareText(Item2.SubItems[ix],Item1.SubItems[ix]);
end;
end;
procedure Tmainform.ListView9ColumnClick(Sender: TObject;
Column: TListColumn);
begin
ColumnToSort := Column.Index;
SortStyle := Not SortStyle;
(Sender as TCustomListView).AlphaSort;
end;
我是这么排序的。你点击那一列都可以排序了。我用过多次了。