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

结果输出:

posted on 2015-04-10 09:55  Lei zhou  阅读(1259)  评论(0编辑  收藏  举报