1 先写一个Comparer类 ListViewItemComparer.
属性 Column 用来记载要进行排列的ListView列的索引。这个属性在执行ColumnClick事件时 通过ColumnClickEventArgs参数赋值。
属性 SortOrder 用来描述排序模式:正序,反序。
属性 SortedType 用来描述是那中数据类型排序,这个枚举包括:Date(日期),Number(数字),Char(字符串),Blend(数字字符串混合类型)
using System;
using System.Collections;
using System.IO;
using System.Windows.Forms;

namespace Windy.Command.WinControl.ListView


{

/**//// <summary>
/// Summary description for ListViewItemComparer.
/// </summary>
public class ListViewItemComparer : IComparer

{
public ListViewItemComparer()

{
}


/**//// <summary>
///
/// </summary>
private int column = 0;

/**//// <summary>
///
/// </summary>
private SortOrder order;

/**//// <summary>
///
/// </summary>
private SortedType sortedtype;



/**//// <summary>
///
/// </summary>
public int Column

{
get

{
return column;
}
set

{
column=value;
}
}

/**//// <summary>
///
/// </summary>
public SortOrder Order

{
get

{
return order;
}
set

{
order=value;
}
}

public SortedType SortType

{
get

{
return sortedtype;
}
set

{
sortedtype = value;
}
}



public int Compare(object x, object y)

{
int retval = 0;

string sx = "";
string sy = "";

ListViewItem lisviewX = (ListViewItem)x;
ListViewItem lisviewY = (ListViewItem)y;
if (column>=lisviewX.SubItems.Count)

{
sx = "";
}
else

{
sx = ( (ListViewItem)x ).SubItems[column].Text;
}
if (column>=lisviewY.SubItems.Count)

{
sy = "";
}
else

{
sy = ( (ListViewItem)y ).SubItems[column].Text;
}

switch(SortType)

{
case SortedType.Blend:

{
char[] chrx = sx.ToCharArray();
char[] chry = sy.ToCharArray();

int charXValueCount = 0;
int charYValueCount = 0;

foreach( char c in chrx )

{
charXValueCount += c;
}

foreach( char c in chry )

{
charYValueCount += c;
}

if ( charXValueCount > charYValueCount )

{
retval = 1;
}
else

{
retval = -1;
}

}
break;
case SortedType.Char:

{
try

{
retval = String.Compare(sx,sy);
}
catch

{
retval = Comparer.Default.Compare(x,y);
}
}
break;
case SortedType.Date:

{
try

{
DateTime dtx = Convert.ToDateTime(sx);
DateTime dty = Convert.ToDateTime(sy);
retval = DateTime.Compare(dtx,dty);
}
catch

{
retval = String.Compare(sx,sy);
}
}
break;
case SortedType.Number:

{

try

{
Decimal dx = Convert.ToDecimal(sx);
Decimal dy = Convert.ToDecimal(sy);
retval = Decimal.Compare(dx,dy);
}
catch

{
retval = Comparer.Default.Compare(sx,sy);
}

}
break;
default:
break;
}

if( order == SortOrder.Ascending )

{
return retval;
}
else if( order==SortOrder.Descending )

{
return -retval;
}
else

{
return 0;
}
}

}


/**//// <summary>
///
/// </summary>
public enum SortedType

{
Date,Number,Char,Blend
}
}

2 调用的时候:属性 SortedType 用来描述是那中数据类型排序,这个枚举包括:Date(日期),Number(数字),Char(字符串),Blend(数字字符串混合类型)



































































































































































































































































































先创建ListViewItemComparer 实体:
ListViewItemComparer comparer = new ListViewItemComparer();
设置ListView 的 ListViewItemSorter属性为 comparer 。
处理ListView的ColumnClick事件:
这里唯一需要做的就是把你想要排序的列的数据类型“人为”
分析出来,然后给comparer 的SortType 属性赋相应的值。最后
执行ListView的Sort()方法。
PS:代码没写注解,如有疑问和我联系。











































