嵌套Dictionary的遍历与排序(按Key值)(二)

class Program
    {
        static void Main()
        {
           
            //构造一个嵌套字典
            Dictionary<int, Dictionary<int, string>> dicExternal = new Dictionary<int, Dictionary<int, string>>();
            for (int i = 90; i > 65; i = i - 5)
            {
                Dictionary<int, string> dicInternal = new Dictionary<int, string>();
                for (int j = i; j > i - 5; j--)
                {
                    char asc = (char)j;
                    dicInternal.Add(j, asc.ToString());
                }
                dicExternal.Add(i, dicInternal);
            }
            //内层字典排序
            Dictionary<int, Dictionary<int, string>> T = new Dictionary<int, Dictionary<int, string>>();
            foreach (int key in dicExternal.Keys)
            {
                Dictionary<int, string> dicOrderInter = new Dictionary<int, string>();
                dicOrderInter = (from p in dicExternal[key] orderby p.Key select p).ToDictionary(pair => pair.Key, pair => pair.Value);
                T.Add(key, dicOrderInter);
            }
            //外层字典排序
            Dictionary<int, Dictionary<int, string>> dicOrderExter = new Dictionary<int, Dictionary<int, string>>();
            dicOrderExter = (from p in T orderby p.Key select p).ToDictionary(pair => pair.Key, pair => pair.Value);

            //输出排序后的所有内层字典的Value
            foreach (int key in dicOrderExter.Keys)
            {
                foreach (int keyInter in dicOrderExter[key].Keys)
                {
                    Console.WriteLine("key:" + keyInter + ",Value:" + dicOrderExter[key][keyInter].ToString());
                }
            }
            Console.ReadLine();
           
        }
    }

运行结果:

 
 
 
 
posted @ 2013-05-24 15:44  RichZhang  阅读(247)  评论(0编辑  收藏  举报