Articles   Members Online: 3
-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
How to sort a TListView by numerical values 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
31-Oct-02
Category
VCL-General
Language
Delphi 2.x
Views
56
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

My TListView is populated with file names in the first column and their sizes in 
the second column. To sort the data in the columns I use certain code. Now I would 
like the second column sorted not by Ansi values, where 822 is greater than 1000, 
but by numerical values. The second column must behave like the first, which means 
that the first click sorts the values ascending and the next click sorts them 
descending. Can anyone help me?

Answer:

Start a new project, and drop a ListView on the form. Replace all of the unit's 
code with code listed below. Set the ListView1's OnColumnClick Event to point to 
the ListView1ColumnClick in the code and link the form's OnCreate to the 
FormCreate. Run the program. Every time you click on the "Column 2" header, the 
column will sort in the opposite order. Note, if you click too quick, it'll be 
considered a double click, and it's not set up for that. Also, if you click on 
Column 1's header, it'll sort alphabetically.

1   unit Unit1;
2   
3   {$O-}
4   
5   interface
6   
7   uses
8     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
9   ComCtrls,
10      StdCtrls;
11  
12  type
13    TForm1 = class(TForm)
14      ListView1: TListView;
15      procedure FormCreate(Sender: TObject);
16      procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
17    private
18      NameSortOrder, SizeSortOrder: integer;
19    public
20    end;
21  
22  var
23    Form1: TForm1;
24  
25  implementation
26  
27  {$R *.DFM}
28  
29  procedure TForm1.FormCreate(Sender: TObject);
30  var
31    i: Integer;
32    ListItem: TListItem;
33  begin
34    Randomize;
35    NameSortOrder := -1;
36    SizeSortOrder := -1;
37    with ListView1 do
38    begin
39      Align := alClient;
40      ViewStyle := vsReport;
41      with Columns.Add do
42      begin
43        Caption := 'Column 1';
44        width := 90;
45      end;
46      with Columns.Add do
47      begin
48        Caption := 'Column 2';
49        width := 90;
50        Alignment := taRightJustify;
51      end;
52      for i := 1 to 20 do
53      begin
54        ListItem := ListView1.Items.Add;
55        with ListItem do
56        begin
57          caption := format('FileName %2d', [i]);
58          SubItems.add(Format('%3.6n', [random]));
59        end;
60      end;
61    end;
62  end;
63  
64  function SizeCustomSort(Item1, Item2: TListItem; ParamSort: integer): integer;
65    stdcall;
66  var
67    R1, R2: real;
68    code: Integer;
69  begin
70    val(Item1.SubItems.Strings[0], R1, code); {ignore code}
71    val(Item2.SubItems.Strings[0], R2, code);
72    if R1 > R2 then
73      result := ParamSort
74    else if R1 < R2 then
75      result := -ParamSort
76    else
77      result := 0;
78  end;
79  
80  procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);
81  begin
82    if Column.Index = 1 then
83    begin
84      ListView1.CustomSort(@SizeCustomSort, SizeSortOrder);
85      SizeSortOrder := SizeSortOrder * -1; {reverse the next sort order}
86    end
87    else
88    begin
89      ListView1.SortType := stText;
90      ListView1.AlphaSort;
91    end;
92  end;
93  
94  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