111,097
社区成员




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ListView//根据不同的项目,更改成对应项目的名称
{
class ListViewItemComparer : IComparer
{
private int column;
private bool numeric = false;
public int Column
{
get {return column ;}
set { column = value; }
}
public bool Numeric
{
get { return numeric; }
set { numeric = value; }
}
public ListViewItemComparer(int columnIndex)
{
Column = columnIndex;
}
public int Compare(object x, Object y)
{
ListViewItem listX = (ListViewItem)x;
ListViewItem listY = (ListViewItem)y;
if (Numeric)
{
//Convert Column text to numbers before comparing
//If the vonersion fails,just use the value 0.
decimal listXVal, listYVal;
try
{
listXVal = decimal.Parse(listX.SubItems[Column].Text);
}
catch
{
listXVal = 0;
}
try
{
listYVal = decimal.Parse(listY.SubItems[Column].Text);
}
catch
{
listYVal = 0;
}
return decimal.Compare(listXVal, listYVal);
}
else
{
//Keep the column text in its native string format
//and perform an alphavetic comparison.
string listXText = listX.SubItems[Column].Text;
string listyText = listY.SubItems[Column].Text;
return string.Compare(listXText, listXText);
}
}
}
}