ListView排序
public class ListViewSorter : IComparer
{
//get or set which column we use to sort
private int m_iColumnToSort;
public int SortColumn
{
get { return m_iColumnToSort; }
set { m_iColumnToSort = value; }
}
//get or set order of sorting
private System.Windows.Forms.SortOrder m_oOrderToSort;
public System.Windows.Forms.SortOrder SortOrder
{
get { return m_oOrderToSort; }
set { m_oOrderToSort = value; }
}
public ListViewSorter()
{
m_iColumnToSort = frmExplorer.g_iColumn;
m_oOrderToSort = frmExplorer.g_oOrder;
}
public ListViewSorter(int column, System.Windows.Forms.SortOrder order)
{
m_iColumnToSort = column;
m_oOrderToSort = order;
}
//override the Camparer method
public int Compare(object x, object y)
{
int iResult;
ListViewItem lvItemX, lvItemY;
lvItemX = (ListViewItem)x;
lvItemY = (ListViewItem)y;
iResult = string.Compare(lvItemX.SubItems[m_iColumnToSort].Text, lvItemY.SubItems[m_iColumnToSort].Text);
if (m_oOrderToSort == System.Windows.Forms.SortOrder.Ascending)
{
return iResult;
}
else if (m_oOrderToSort == System.Windows.Forms.SortOrder.Descending)
{
return (-iResult);
}
else
{
return 0;
}
}
}