问题
I am using Delphi 2010 and TListView to list Names and other data. The first two columns is the Last Name & First Name
Caption = Last Name
SubItems[0] = First Name
How do I sort the ListView by these two columns? These will only be the columns the Listview will be sorted by and I would like to always keep the sort as such (when adding, editing, deleting items)
How can I accomplish this?
回答1:
Set SortType to 'stBoth', and implement an OnCompare event handler. Example:
procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
  Data: Integer; var Compare: Integer);
var
  S1, S2: string;
begin
  S1 := Item1.Caption;
  if Item1.SubItems.Count > 0 then
    S1 := S1 + Item1.SubItems[0];
  S2 := Item2.Caption;
  if Item2.SubItems.Count > 0 then
    S2 := S2 + Item2.SubItems[0];
  Compare := CompareText(S1, S2);
end;
来源:https://stackoverflow.com/questions/15057886/tlistview-column-sort-sort-by-first-two-columns