zoukankan      html  css  js  c++  java
  • Dictionary学习笔记嵌套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();
               
            }
        }

    运行结果:

  • 相关阅读:
    [后缀数组] Luogu P5028 Annihilate
    [后缀数组] Luogu P3809 后缀排序
    [差分][线段树] Luogu P4243 等差数列
    [线段树] Luogu P4314 COU监控
    [二分][dp凸优化] Luogu P4383 林克卡特树lct
    [树上差分][dfs] Luogu P4652 One-Way Streets
    [dfs][思维] Luogu P3208 矩阵
    [dfs][二进制状压] Luogu P4906 小奔关闹钟
    [容斥] Luogu P5339 唱、跳、rap和篮球
    [dfs][模拟网络流] Luogu P4189 星际旅行
  • 原文地址:https://www.cnblogs.com/wangchao928/p/2508473.html
Copyright © 2011-2022 走看看