zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    Identifier expected after this token
    需要整理的
    Context
    SharedPreferences
    一些常规注意事项
    一个点亮屏幕的service
    BroadcastReceiver中调用Service
    BroadcastReceiver
    Service
    微服务简介
  • 原文地址:https://www.cnblogs.com/imihiroblog/p/2590562.html
Copyright © 2011-2022 走看看