zoukankan      html  css  js  c++  java
  • C#集合Dictionary中按值的排序

    C#集合Dictionary中按值的降序排列

     static void Main(string[] args)
    {
                Dictionary<string, int> dt = new Dictionary<string, int>();
                dt.Add("美丽",2);
                dt.Add("校园",1);
                dt.Add("大学",3);

                var result1 = from pair in dt select pair;
                foreach (KeyValuePair<string, int> pair in result1)
                {
                    Console.WriteLine(pair.Key.ToString() + " " + pair.Value.ToString());
                }
                Console.WriteLine();

                var result2 = from pair in dt orderby pair.Value select pair;
                foreach (KeyValuePair<string, int> pair in result2)
                {
                    Console.WriteLine(pair.Key.ToString()+" "+pair.Value.ToString());
                }
                Console.WriteLine();

                var result3 = from pair in dt orderby pair.Value descending select pair;
                foreach (KeyValuePair<string, int> pair in result3)
                {
                    Console.WriteLine(pair.Key.ToString() + " " + pair.Value.ToString());
                }
                Console.Read();
        }

    结果输出:

  • 相关阅读:
    8 组合
    7 继承与派生
    防止伪造跨站请求(转)
    HTML5 拖拽效果实现
    yii2.0 文件上传
    div光标定位问题总结
    可编辑DIV (contenteditable="true") 在鼠标光标处插入图片或者文字
    Linux下搭建Nginx+MySQL+PHP
    GitHub初体验
    Mac下配置Apache + Php + Mysql环境
  • 原文地址:https://www.cnblogs.com/lezhou2014/p/4413735.html
Copyright © 2011-2022 走看看