当前位置: 编程技术>.net/c#/asp.net
C# List排序的实例介绍
来源: 互联网 发布时间:2014-08-30
本文导语: 说明:List类型可以支持对任意类型的存储,也可以对其进行排序。 以下为大家介绍一种简单的排序实现方法。 1、定义一个类CAttributeFeature,后面用List存储该类的列表。 public class CAttributeFeature { public string m_strAttributeName { ge...
说明:List类型可以支持对任意类型的存储,也可以对其进行排序。
以下为大家介绍一种简单的排序实现方法。
1、定义一个类CAttributeFeature,后面用List存储该类的列表。
public class CAttributeFeature
{
public string m_strAttributeName { get; set; }
public double m_dAttributeFeature { get; set; }
public CAttributeFeature(string strName, double dFeature)
{
this.m_strAttributeName = strName;
this.m_dAttributeFeature = dFeature;
}
public void FeatureAdd(double dFeature)
{
this.m_dAttributeFeature += dFeature;
}
}
2、定义一个函数SortCompare(),对List进行排序时作为参数使用:
#region SortCompare()函数,对List进行排序时作为参数使用
///
/// 对List进行排序时作为参数使用
///
///
///
///
///by http://www.
private static int SortCompare(CAttributeFeature AF1, CAttributeFeature AF2)
{
int res = 0;
if (AF1.m_dAttributeFeature > AF2.m_dAttributeFeature)
{
res = -1;
}
else if (AF1.m_dAttributeFeature < AF2.m_dAttributeFeature)
{
res = 1;
}
return res;
}
#endregion
3、产生一个List的对象,将前一步定义的SortCompare()函数做为Sort()方法的参数传入,对List进行排序。
List listAF = m_nDTreeGenerator1.Chaos_GetUsefulAttributeFeature(Chaos_DTree1); //按其特征值进行排序 listAF.Sort(SortCompare);