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];
Result := +CompareText(Item1.Caption,Item2.Caption);
end;
end;
//调用
procedure TmForm.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
ListView1.CustomSort(@CustomSortProc, Column.Index);
end;
//建回调函数
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];
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 TmForm.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
ListView1.CustomSort(@CustomSortProc, Column.Index);
end;
function SortByCaption(Item1, Item2: TListItem;
ParamSort: integer): integer; stdcall;
begin
Result := compare(item1.caption, item2.caption);
if ParamSort=0 then Result := -Result;
end;
procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
var
SortProc: TLVCompare;
begin
Column.Tag := (Column.Tag +1)mod 2;
case Column.Index of
0: SortProc := @SortByColumnCaption;
1: SortProc := @SortByColumnSubitem0;
2: SortProc := @SortByColumnSubitem1;
else SortProc := nil;
end;
if Assigned(SortProc) then
ListView1.CustomSort(SortProc, Column.Tag);
end;