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

 

转载地址: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();
    }

 

posted @ 2016-03-02 17:50  zh89233  阅读(561)  评论(0编辑  收藏  举报