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
How to sort a TListView column with integers and separators on column click 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
Sort a TListView column with integer values on column click 06-Sep-04
Category
VCL-General
Language
Delphi 7.x
Views
418
User Rating
9
# Votes
1
Replies
0
Publisher:
Libby, Alex
Reference URL:
Alex Libby
			Ever tried sorting numbers ? Yes ?

Have you ever tried sorting numbers, with thousand separators, and in numerical 
order ?

A little harder, I think ! I found the same whilst writing some code for a 
program,until I discovered a way to do it. The slow way is to convert the existing 
string in the TListView to remove the separator, sort it, and then re-insert the 
separator. 

However, there is another way. It is based on the method above, but with one 
crucial difference. The trick is to convert the numbers, but then to assign it to a 
temporary double-length integer, run the sort, and output the result. The 
separators in the numbers shown on screen are never removed - their order is 
changed based on the temporary conversion. Below is some example code - it uses 
example filenames and filesizes to demonstrate the sorting procedure.

To see this in action, you need to:

1. Start a new project, and add a TListView to a TForm.
2. Add a few example filenames in column 0, then some example filesizes in column 1.
3. Add / change the code, to match the code below.
4. Run the project.

Notes:
- The code can be adapted to use different columns, depending on your requirements 
- as long as the right column numbers are assigned, then it should still work OK. 
- To change the direction of the sorting on the first click, change the 
ListView1.Tag numbers in the Object Inspector.
- ListView1.Tag numbers: -1 = reverse order; 1 = forward order.
- It should be noted that the ',' symbol is being used as the thousand separator - 
this can also be changed from the sThouSep constant declaration




CODE FOR SORTING NUMBERS WITH SEPARATORS:


1   unit cdSortNumbers;
2   
3   interface
4   
5   uses
6     SysUtils, Classes, Forms, Controls, ComCtrls;
7   
8   type
9     TForm1 = class(TForm)
10      ListView1: TListView;
11      procedure bySizeClick(Sender: TObject);
12      procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
13    private
14      { Private declarations }
15    public
16      { Public declarations }
17    end;
18  
19  var
20    Form1 : TForm1;     { main form }
21    n1    : double;     { used to assign converted numbers to double length integers, 
22  before conversion }
23    n2    : double;     { n1 = first number in pair, n2 = second number in pair }
24  
25  const
26    sThouSep = ',';     { defines the thousand separator being used in TListView - 
27  change if required }
28  
29  implementation
30  
31  {$R *.dfm}
32  
33  
34  { main sorting function - convert strings to numbers, then sort accordingly }
35  function CustomSizeSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; 
36  stdcall;
37  begin
38    n1 := 0;
39    n2 := 0;
40  
41    { string conversion and assignment process to n1 or n2, based on order being 
42  sorted }
43    if ParamSort = 1 then
44      begin
45        n1 := StrToFloat(StringReplace(Item1.SubItems.Strings[0], sThouSep, '', 
46  [rfReplaceAll]));
47        n2 := StrToFloat(StringReplace(Item2.SubItems.Strings[0], sThouSep, '', 
48  [rfReplaceAll]));
49      end
50    else if ParamSort = -1 then
51      begin
52        n1 := StrToFloat(StringReplace(Item2.SubItems.Strings[0], sThouSep, '', 
53  [rfReplaceAll]));
54        n2 := StrToFloat(StringReplace(Item1.SubItems.Strings[0], sThouSep, '', 
55  [rfReplaceAll]));
56      end;
57  
58    { determines final position, based on comparing results from conversion process }
59    if n1 > n2 then Result := 1 else if n1 < n2 then Result := -1 else Result := 0;
60  end;
61  
62  { determine direction of sort, then call sort function }
63  { can be called from other components, such as a TButton }
64  procedure TForm1.bySizeClick(Sender: TObject);
65  begin
66    if ListView1.Tag = -1 then ListView1.Tag := 1 else ListView1.Tag := -1;
67    ListView1.CustomSort(@CustomSizeSortProc, ListView1.Tag);
68  end;
69  
70  { optional extra - calls sorting routine from column header click }
71  { if TListView being used to sort many numbers / strings, then these can be added 
72  in a similar manner }
73  procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);
74  begin
75    if column = ListView1.Column[1] then bySizeClick(Sender);
76  end;
77  
78  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