码迷,mamicode.com
首页 > 编程语言 > 详细

C#集合Dictionary中按值的排序

时间:2015-04-10 10:54:59      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

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/lezhou2014/p/4413735.html

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