C#的MSDN只能对每列当作string类型排列,而对int类型,排序有bug,比如排序的结果可能是1,10,11,2,3....,而准确的排序应该是1,2,3..
本文的算法已对ListView排序算法进行了修改和完善。
初始化:
private int sortColumn = -1;
ListView排序对应的事件:
private void listViewData_ColumnClick(object sender, ColumnClickEventArgs e)
{
if (e.Column != sortColumn)
{
// Set the sort column to the new column.
sortColumn = e.Column;
// Set the sort order to ascending by default.
{
if (e.Column != sortColumn)
{
// Set the sort column to the new column.
sortColumn = e.Column;
// Set the sort order to ascending by default.
listViewData.Sorting = SortOrder.Ascending;
}
else
{
// Determine what the last sort order was and change it.
if (listViewData.Sorting == SortOrder.Ascending)
listViewData.Sorting = SortOrder.Descending;
else
listViewData.Sorting = SortOrder.Ascending;
}
}
else
{
// Determine what the last sort order was and change it.
if (listViewData.Sorting == SortOrder.Ascending)
listViewData.Sorting = SortOrder.Descending;
else
listViewData.Sorting = SortOrder.Ascending;
}
// Call the sort method to manually sort.
listViewData.Sort();
// Set the ListViewItemSorter property to a new ListViewItemComparer
// object.
this.listViewData.ListViewItemSorter = new ListViewItemComparer(e.Column,listVie
listViewData.Sort();
// Set the ListViewItemSorter property to a new ListViewItemComparer
// object.
this.listViewData.ListViewItemSorter = new ListViewItemComparer(e.Column,listVie
wData.Sorting);
}
}
用于ListView排序的类:
// Implements the manual sorting of items by columns.
class ListViewItemComparer : IComparer
{
private int col;
private SortOrder order;
public ListViewItemComparer()
{
col = 0;
order = SortOrder.Ascending;
}
public ListViewItemComparer(int column, SortOrder order)
class ListViewItemComparer : IComparer
{
private int col;
private SortOrder order;
public ListViewItemComparer()
{
col = 0;
order = SortOrder.Ascending;
}
public ListViewItemComparer(int column, SortOrder order)
{
col = column;
der = order;
}
public int Compare(object x, object y)
{
int returnVal = -1;
col = column;
der = order;
}
public int Compare(object x, object y)
{
int returnVal = -1;
int a = 0, b = 0;
if (int.TryParse(((ListViewItem)x).SubItems[col].Text, out a) && int.TryParse(((ListViewItem)y).SubItems[col].Text, out b))
{
returnVal = a >= b ? (a == b ? 0 : 1) : -1;
}
else
if (int.TryParse(((ListViewItem)x).SubItems[col].Text, out a) && int.TryParse(((ListViewItem)y).SubItems[col].Text, out b))
{
returnVal = a >= b ? (a == b ? 0 : 1) : -1;
}
else
{
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.
sorting out 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.
sorting out returnVal *= -1;
}
return returnVal;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论