.net3.5下利用Linq新特性对Dictionary进行快速排序

来源:互联网 发布:linux怎么回到目录下一 编辑:程序博客网 时间:2024/05/21 02:50

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace DictionarySorting
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<int, int> dic = new Dictionary<int, int>();
            dic.Add(1, 158);
            dic.Add(5, 25);
            dic.Add(3, 215);
            dic.Add(2, 369);
            dic.Add(4, 147);

            var result = from pair in dic orderby pair.Value select pair;

            foreach (KeyValuePair<int, int> pair in result)
            {
                Console.WriteLine("Key:{0}, Value:{1}", pair.Key, pair.Value);
            }

            Console.ReadKey();
        }
    }
}

原创粉丝点击