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

    运行结果:

     
     
     
     
  • 相关阅读:
    数据结构与算法
    android vitamio的坑
    Fragment的坑
    scrollview里面嵌套listview的坑
    关于微信小程序的tabbar
    Intent初级学习
    HDU 4562 守护雅典娜
    SGU 542 Gena vs Petya
    [二] EditText and Button
    A Data Structure for Dynamic Trees
  • 原文地址:https://www.cnblogs.com/richzhang/p/3097019.html
Copyright © 2020-2023  润新知