Board index » delphi » TListView Sorting

TListView Sorting

I am using a TListView to show data in report mode.  I need to sort
subcolumns when the ColumnClick event is fired.  Has anyone written an
algorithm to do this type of sorting?

Greatly appreciated.

Chris Woodruff
chris...@carecentric.com

PS - Reply to email also please.

 

Re:TListView Sorting


Quote
Chris Woodruff wrote:

> I am using a TListView to show data in report mode.  I need to sort
> subcolumns when the ColumnClick event is fired.  Has anyone written an
> algorithm to do this type of sorting?

Hi Chris,

If you want to do something like sorting the entire listbox on the
column clicked on then the following code should do the trick:

// Sort alphabetically. Note this is case-sensitive; use lstrcmpi for
// case-insensitive comparison.
function MySort(Item1, Item2: TListItem; ColNum: integer): integer;
stdcall;
begin
  if ColNum = 0 then
    Result := lstrcmp(PChar(Item1.Caption), PChar(Item2.Caption))
  else
    Result := lstrcmp(PChar(Item1.SubItems[ColNum-1]),
PChar(Item2.SubItems[ColNum-1]));
end;

procedure TForm1.ListView1ColumnClick(Sender: TObject;
  Column: TListColumn);
begin
  ListView1.CustomSort(@MySort, Column.Index);
end;

Regards
Chris Jobson

Other Threads