473,241 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

sorting numerical values in a listview

Hi,

I have a listview which has columns of text and columns of numerical
data.
I need to be able to sort these columns into ascending/desending order
whenever the user clicks on the column header.
The text columns are sorted correctly but the numerical columns seem
not to get sorted properly.

For example I get the following result when I try to sort a numerical
column.
0
1
10
11
12
2
20
21
22
3
30
31

etc
etc

Here is the following code I am using to do the sorting

private void listView1_ColumnClick(object sender,
ColumnClickEventArgs e)
{ // Determine whether the column is the same as the
last column clicked.
if (e.Column != sortColumn)
{
// Set the sort column to the new column.
sortColumn = e.Column;
// Set the sort order to ascending by default.
listView1.Sorting = SortOrder.Ascending;
}
else
{
// Determine what the last sort order was and change
it.
if (listView1.Sorting == SortOrder.Ascending)
listView1.Sorting = SortOrder.Descending;
else
listView1.Sorting = SortOrder.Ascending;
}

// Call the sort method to manually sort.
listView1.Sort();
// Set the ListViewItemSorter property to a new
ListViewItemComparer
// object.
this.listView1.ListViewItemSorter = new
ListViewItemComparer(e.Column,

listView1.Sorting);
}
class ListViewItemComparer : IComparer
{
private int col;
private SortOrder order;
public ListViewItemComparer()
{
col = 0;
order = SortOrder.Ascending;
}

public ListViewItemComparer(int column, SortOrder order)
{
col = column;
this.order = order;
}

public int Compare(object x, object y)
{
int returnVal= -1;
returnVal =
String.Compare(((ListViewItem)x).SubItems[col].Text,
((ListViewItem)y).SubItems[col].Text);
// Determine whether the sort order is descending.
if (order == SortOrder.Descending)
{
// Invert the value returned by String.Compare.
returnVal *= -1;
}
return returnVal;
}
}

Jul 3 '06 #1
2 7154
"jediknight" <wa*****@gmail.coma écrit dans le message de news:
11*********************@m79g2000cwm.googlegroups.c om...

| I have a listview which has columns of text and columns of numerical
| data.
| I need to be able to sort these columns into ascending/desending order
| whenever the user clicks on the column header.
| The text columns are sorted correctly but the numerical columns seem
| not to get sorted properly.

Why not use a List<intto hold the values and set that to be the DataSource
for the listview ? That way the list is a true list of integers instead of
strings and you can add, remove and sort the list; the view then simply
reflects the contents of the list.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jul 3 '06 #2
jediknight wrote:
String.Compare(((ListViewItem)x).SubItems[col].Text,
((ListViewItem)y).SubItems[col].Text);
Well, clearly, you are comparing the values as strings, which would
give you exactly the results you are seeing. So, we need to compare
that column as ints:

string x1 = ((ListViewItem)x).SubItems[col].Text;
string y1 = ((ListViewItem)y).SubItems[col].Text;

int returnVal = -1;
if (col == 5) // assumes numeric data in fifth column.
returnVal = Convert.ToInt32(x1).CompareTo(COnvert.ToInt32(y1)) ;
else
returnVal = String.Compare(x1, y1);

Jul 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: rodger | last post by:
Hi all, how do I set a Textbox to only accept numerical values in vb .net many thanks rodger
2
by: Martin McCormick | last post by:
Most of the C programming I write manipulates strings so I haven't had much experience with numerical functions and passing variables in and out which brings me to a laughably simple problem that I...
5
by: Dragan Matic | last post by:
If I have a table t with column c which is defined as char(5) and fill it with following values: insert into t (c) values (' 1') insert into t (c) values (' 2') insert into t (c) values...
1
by: Dragan Matic | last post by:
If I have a table t with column c which is defined as char(5) and fill it with following values: insert into t (c) values (' 1') insert into t (c) values (' 2') insert into t (c) values...
2
by: yxq | last post by:
Hello, By default, when set the listview's property "Sorting=Descending", the listview will sort the items base on the string in first column(column(0)) while adding items, how to specify listview...
4
by: Kheyalimitra | last post by:
Hi,I am Kheyali.I want to know that in php how to show numerical values from database?
7
by: pt36 | last post by:
Hi I have a php string like this: $string = "one two three four five" I have to sorting the values randomly every time the page are loaded. So, for example: first time "one two three four five"...
3
by: sannihith | last post by:
how do I set a Textbox to only accept numerical values in vb .net
4
by: gio1405 | last post by:
I am kind of a beginner in C# and I have to do a program in which i have to add all the numerical values of the text boxes. My code starts like this: float.nTxtbox1 = float.parse (txtbox1.text); ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.