Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
ow to sorting a TListView by the column clicked by the user Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
27-May-03
Category
VCL-General
Language
Delphi 3.x
Views
143
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Ernesto De Spirito

How to sort a ListView in ascending or descending order by a given column?

Answer:

Solve 1:

We want the following behaviour for a ListView: 


When the user clicks on a column header, the ListView should be sorted by that 
column 

The initial sort order should be ascending. If the user clicks on the same column 
again, the sort order should be toggled. If the user clicks on another column, the 
sort order of the new column should be the same as the last sorted column.


For the implementation we need two variables to hold the last column clicked by the 
user and the current sort order: 

1   var
2     LastSortedColumn: integer;
3     Ascending: boolean;
4   
5   We can initialize them when the form is created: 
6   
7   procedure TForm1.FormCreate(Sender: TObject);
8   begin
9     LastSortedColumn := -1;
10    Ascending := True;
11  end;
12  
13  in the ColumnClick event of the ListView we determine the sort order and perform 
14  the sort: 
15  
16  procedure TForm1.ListView1ColumnClick(Sender: TObject;
17    Column: TListColumn);
18  begin
19    if Column.Index = LastSortedColumn then
20      Ascending := not Ascending
21    else
22      LastSortedColumn := Column.Index;
23    TListView(Sender).CustomSort(@SortByColumn, Column.Index);
24  end;


SortByColumn is a function that should be previously declared and is the function 
used by CustomSort to compare two items. The value passed to the Data parameter of 
CustomSort will be passed as the Data parameter to SortByColumn and we use it for 
the sort column: 
25  
26  function SortByColumn(Item1, Item2: TListItem; Data: integer):
27    integer; stdcall;
28  begin
29    if Data = 0 then
30      Result := AnsiCompareText(Item1.Caption, Item2.Caption)
31    else
32      Result := AnsiCompareText(Item1.SubItems[Data - 1],
33        Item2.SubItems[Data - 1]);
34    if not Ascending then
35      Result := -Result;
36  end;


Solve 2:

Instead of calling the custum sort procedure we also can use the OnColumnClick and 
the OnCompare events ion conjunction... That way the compare routine is kept within 
the class, here a TForm decedant. 

Like this: 
37  
38  procedure TfrmMain.lvThingyColumnClick(Sender: TObject; Column: TListColumn);
39  begin
40    if Column.Index = fColumnClicked then
41      fSortAscending := not fSortAscending
42    else
43    begin
44      fSortAscending := true;
45      fColumnClicked := Column.Index;
46    end; // else
47  
48    Screen.Cursor := crHourglass;
49    try
50      (Sender as TListView).AlphaSort;
51    finally
52      Screen.Cursor := crDefault;
53    end; // try
54  end;
55  
56  procedure TfrmMain.lvThingyCompare(Sender: TObject; Item1, Item2: TListItem; Data:
57    Integer; var Compare: Integer);
58  begin
59    case fColumnClicked of
60      0: Compare := CompareStr(Item1.Caption, Item2.Caption);
61    else
62      Compare := CompareStr(Item1.SubItems[fColumnClicked - 1],
63        Item2.SubItems[fColumnClicked - 1]);
64    end; // case
65    if not fSortAscending then
66      Compare := -Compare;
67  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC