现在的位置: 首页 > 综合 > 正文

C# List.Sort() 排序; DataView 对DataTable 排序

2012年09月19日 ⁄ 综合 ⁄ 共 687字 ⁄ 字号 评论关闭

 

    List<string> sortedHeight = new List<string>();
            foreach (var cs in contourDoc.SelectNodes("Xml/ContourSet"))
            {
                XmlElement tempEle = cs as XmlElement;
                sortedHeight.Add(tempEle.Attributes["height"].Value);
            }
            sortedHeight.Sort(HeightComapare);

 

Sort的参数类型是返回值为int的方法

        private static int HeightComapare(string x, string y)
        {
            return (int.Parse(x) - int.Parse(y));
        }

 

传入值:"0" "180" "30" "90"

返回值:"0" "30" "90" "180"

 

---------------------------------

二、DataView 对DataTable 排序

  //按BuildingName 长度 排序
            dt.Columns.Add("NameLength");
            for (int i = 0; i <= dt.Rows.Count - 1; i++) {
                dt.Rows[i]["NameLength"] = dt.Rows[i]["BuildingName"].ToString().Length;
            }
            DataView tempDv = dt.DefaultView;
            tempDv.Sort = "NameLength asc";//变更DataView的顺序
            dt=tempDv.ToTable();//从DV生成dataTable

 

抱歉!评论已关闭.