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