function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
implementation//之前定义上面这句
procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
ListData.CustomSort(@CustomSortProc, Column.Index);
end;
function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
begin
if ParamSort>0 then
Result := CompareText(Item1.SubItems.Strings[ParamSort-1],Item2.SubItems.Strings[ParamSort-1])
else
Result := CompareText(Item1.Caption,Item2.Caption);
end;
begin
ColumnToSort := Column.Index;
(Sender as TCustomListView).AlphaSort;
end;
The OnCompare event handler causes the list view to sort on the selected column:
procedure TForm1.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;