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

 

    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

 

posted on 2012-07-13 17:23  imihiro  阅读(2470)  评论(0编辑  收藏  举报