List与自定义类的排序一例

2420阅读 0评论2016-06-20 dyli2000
分类:C#/.net


  1. public static List<CLocalTabStruct.ScreenLableItem> GetXmlLabelNameList(string fileFullName)
  2.         {
  3.             List<CLocalTabStruct.ScreenLableItem> listLabelItem = new List<CLocalTabStruct.ScreenLableItem>();
  4.             if (fileFullName.Equals(string.Empty))
  5.                 return null;

  6.             XmlDocument tmpDoc = new XmlDocument();
  7.             tmpDoc.Load(fileFullName);
  8.             XmlNode rootNode = tmpDoc.SelectSingleNode("Controls");

  9.             XmlNodeList slblNodeList = null;
  10.             if (rootNode.SelectSingleNode("SmartLabels") != null)
  11.                 slblNodeList = rootNode.SelectSingleNode("SmartLabels").ChildNodes;

  12.             if (slblNodeList == null)
  13.                 return null;

  14.             foreach (XmlNode xn0 in slblNodeList)
  15.             {
  16.                 XmlElement xe = (XmlElement)xn0;
  17.                 CLocalTabStruct.ScreenLableItem lblItem = new CLocalTabStruct.ScreenLableItem();
  18.                 lblItem.ItemName = xe.GetAttribute("Name");
  19.                 lblItem.Text = xe.GetAttribute("Text");

  20.                 listLabelItem.Add(lblItem);
  21.             }
  22.             listLabelItem.Sort(CompareObject);

  23.             return listLabelItem;
  24.         }

  25.         public static int CompareObject(CLocalTabStruct.ScreenLableItem x, CLocalTabStruct.ScreenLableItem y)
  26.         {
  27.             int result = 0;
  28.             result = y.ItemName.CompareTo(x.ItemName);
  29.             return result;
  30.         }
参考文献:
http://blog.csdn.net/zhouqinghe24/article/details/8649521
上一篇:C# synchronize scroll and select of two DataGridViews
下一篇:让窗体所有控件无效但close有效