我們知道List<>類型可以支持對任意類型的存儲,我們也可以對其進行排序。現介紹一種較為簡單的方法:
1、我們先定義一個類CAttributeFeature,后面我們用List<CAttributeFeature>來存儲該類的一個列表,代碼如下:
1: public class CAttributeFeature
2: {
3: public string m_strAttributeName { get; set; }
4: public double m_dAttributeFeature { get; set; }
5:
6: public CAttributeFeature(string strName, double dFeature)
7: {
8: this.m_strAttributeName = strName;
9: this.m_dAttributeFeature = dFeature;
10: }
11:
12: public void FeatureAdd(double dFeature)
13: {
14: this.m_dAttributeFeature += dFeature;
15: }
16: }
2、我們定義一個函數SortCompare(),對List<CAttributeFeature>進行排序時作為參數使用,代碼如下:
1: #region SortCompare()函數,對List<CAttributeFeature>進行排序時作為參數使用
2: /// <summary>
3: /// 對List<CAttributeFeature>進行排序時作為參數使用
4: /// </summary>
5: /// <param name="AF1"></param>
6: /// <param name="AF2"></param>
7: /// <returns></returns>
8: private static int SortCompare(CAttributeFeature AF1, CAttributeFeature AF2)
9: {
10: int res = 0;
11: if (AF1.m_dAttributeFeature > AF2.m_dAttributeFeature)
12: {
13: res = -1;
14: }
15: else if (AF1.m_dAttributeFeature < AF2.m_dAttributeFeature)
16: {
17: res = 1;
18: }
19: return res;
20: }
21: #endregion
3、產生一個List<CAttributeFeature>的對象,將前一步定義的SortCompare()函數做為Sort()方法的參數傳入,就可以對List<CAttributeFeature>進行排序了。代碼如下:
1: List<CAttributeFeature> listAF = m_nDTreeGenerator1.Chaos_GetUsefulAttributeFeature(Chaos_DTree1);
2:
3: //按其特征值進行排序
4: listAF.Sort(SortCompare);