码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-03-02 19:54:53      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

 

转载地址:http://blog.sina.com.cn/s/blog_5c5bc9070100pped.html

 

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();
    }

 

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

标签:

原文地址:http://www.cnblogs.com/love-zf/p/5235914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!