如下类:
internal 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;
if (((ListViewItem) x).SubItems.Count > col && ((ListViewItem) y).SubItems.Count > col)
{
returnVal = String.Compare(((ListViewItem) x).SubItems[ col ].Text,
((ListViewItem) y).SubItems[ col ].Text);
if (order == SortOrder.Descending)
returnVal *= -1;
}
return returnVal;
}
}
调用:第一个参数是排序字段,第二个参数是排序方式(升序,降序)
this.GetCurrentList().ListViewItemSorter = new ListViewItemComparer(2, this.GetCurrentList().Sorting);
this.GetCurrentList().Sort();