By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,302 Members | 3,621 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,302 IT Pros & Developers. It's quick & easy.

sorting numerical values in a listview

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.