Board index » delphi » TListView sorting

TListView sorting

Hi there,

Can anyone tell me how to sort a listview when clicking on a column caption?
(Just like in Windows Explorer).

thx

Andreas Krgersen

 

Re:TListView sorting


Andreas,
Have a look at TCustomListView.CustomSort in the help file.

HTH,
g

Re:TListView sorting


I've already seen that but it is just a callback function where you can
define HOW you want to sort.
But I want to define WHAT to sort, that is the I want to be able to sort the
sub items too.
But I don't see any straightforward way to do that.

"Gregg" <gchristian...@nospamcox.net> schrieb im Newsbeitrag
news:3da44c38@newsgroups.borland.com...

Quote
> Andreas,
> Have a look at TCustomListView.CustomSort in the help file.

> HTH,
> g

Re:TListView sorting


Quote
"Andreas Krgersen" <krueger...@projecteam.de> wrote in message

news:3da44f88$1@newsgroups.borland.com...
Quote
> I've already seen that but it is just a callback function where you can
> define HOW you want to sort.
> But I want to define WHAT to sort, that is the I want to be able to sort
the
> sub items too.
> But I don't see any straightforward way to do that.

You are on your way then! You simply implement a procedure for each column
you want to sort, then in the column click event, call customsort with the
corresponding sort procedure.

This example assumes a ListView in vsReport mode with 2 Columns(Name and
Desc).

function SortByName(Item1, Item2: TListItem; ParamSort: integer): integer;
stdcall;
begin
  Result := CompareText(Item1.Caption,Item2.Caption);
end;

function SortByDesc(Item1, Item2: TListItem; ParamSort: integer): integer;
stdcall;
begin
  Result := CompareText(Item1.SubItems[0],Item2.SubItems[0]);
end;

procedure TForm1.ListView1ColumnClick(Sender: TObject;
  Column: TListColumn);
begin
  case Column.Index of
    0: ListView1.CustomSort(@SortByName, 0);
    1: ListView1.CustomSort(@SortByDesc, 0);
  end;

end;

If you are not comfortable with the Callbacks, you can also use the
OnCompare event as well. There is a good example of how to do that in the
help.

HTH,
g

Re:TListView sorting


Quote
> I've already seen that but it is just a callback function where you can
> define HOW you want to sort.
> But I want to define WHAT to sort, that is the I want to be able to sort
the
> sub items too.
> But I don't see any straightforward way to do that.

-------------------
OnColumnClick, AlphaSort, OnCompare, CompareText example
-------------------
This example shows how to use the OnColumnClick and OnCompare events of a
list view to let users sort the columns in a report-style list view by
clicking on the column headers. This requires a global variable to keep
track of the column that was clicked:

var ColumnToSort: Integer;

The OnColumnClick event handler sets the global variable to indicate the
column to sort and calls AlphaSort:

procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);

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;

end;

Note: This OnCompare event handler uses the global CompareText function. An
application may want to use AnsiCompareText, CompareStr, or AnsiCompareStr
instead, depending on whether the comparison should be case-sensitive and
whether the locale should be considered.

Other Threads