sorting numerical values in a listview

J

jediknight

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;
}
}
 
J

Joanna Carter [TeamB]

"jediknight" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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<int> to 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
 
J

james.curran

jediknight said:
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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top