Dictionary排序

简介: 有时候由于某些要求会对Dictionary排序,一般有两种方法。 1、使用SortedDictionary。 这种自动会对保存的值进行排序。 [csharp] view plaincopyprint? static ...
有时候由于某些要求会对Dictionary排序,一般有两种方法。
1、使用SortedDictionary。
这种自动会对保存的值进行排序。
  1. static void Main(string[] args) 
  2.      SortedDictionary<int, object> testDictioary = new SortedDictionary<int, object>(); 
  3.            int flag = 0; 
  4.  
  5.            do 
  6.            { 
  7.                Random random = new Random(); 
  8.  
  9.                int temp = random.Next(100); 
  10.  
  11.                if (!testDictioary.ContainsKey(temp)) 
  12.                { 
  13.                    testDictioary.Add(temp, null); 
  14.                } 
  15.  
  16. flag = testDictioary.Count;
  17.  
  18.            } while (flag < 20); 
  19.  
  20.            Console.WriteLine("未排序前:"); 
  21.  
  22.            foreach (int key in testDictioary.Keys) 
  23.            { 
  24.                Console.Write(string.Format(@"{0}  ", key)); 
  25.            } 

结果:

2、自己写的方法。如下

  1. public static void Sort(Dictionary<int, object> dictionary) 
  2.        { 
  3.            try 
  4.            { 
  5.                List<int> sortList = new List<int>(); 
  6.  
  7.                Dictionary<int, object> tempDictionary = new Dictionary<int, object>(); 
  8.  
  9.                foreach (int key in dictionary.Keys) 
  10.                { 
  11.                    sortList.Add(key); 
  12.                     
  13.                    tempDictionary.Add(key, dictionary[key]); 
  14.                } 
  15.  
  16.                int flag = 1; 
  17.  
  18.                int i, j; 
  19.  
  20.                int itemCount = sortList.Count; 
  21.  
  22.                int itemTemp; 
  23.  
  24.                for (i = 1; i < itemCount && flag == 1; i++) 
  25.                { 
  26.                    flag = 0; 
  27.  
  28.                    for (j = 0; j < itemCount - i; j++) 
  29.                    { 
  30.                        int countfore = sortList[j]; 
  31.  
  32.                        int countback = sortList[j + 1]; 
  33.  
  34.                        if (countfore > countback) 
  35.                        { 
  36.                            flag = 1; 
  37.  
  38.                            itemTemp = sortList[j]; 
  39.  
  40.                            sortList[j] = sortList[j + 1]; 
  41.  
  42.                            sortList[j + 1] = itemTemp; 
  43.                        } 
  44.                    } 
  45.                } 
  46.                dictionary.Clear(); 
  47.  
  48.                for (int n = 0; n < itemCount; n++) 
  49.                { 
  50.                    foreach (int tempKey in tempDictionary.Keys) 
  51.                    { 
  52.                        int value = sortList[n]; 
  53.  
  54.                        if (tempKey.Equals(value)) 
  55.                        { 
  56.                            if (!dictionary.ContainsKey(tempKey)) 
  57.                            { 
  58.                                dictionary.Add(tempKey, tempDictionary[tempKey]); 
  59.                            } 
  60.                        } 
  61.                    } 
  62.                } 
  63.            } 
  64.            catch { } 
  65.        } 

        调用结果如下:

  1. static void Main(string[] args) 
  2.      { 
  3.          Console.WriteLine("key为数字"); 
  4.  
  5.          Dictionary<int, object> testDictioary = new Dictionary<int, object>(); 
  6.  
  7.          int flag = 0; 
  8.  
  9.          do 
  10.          { 
  11.              Random random = new Random(); 
  12.  
  13.              int temp = random.Next(100); 
  14.  
  15.              if (!testDictioary.ContainsKey(temp)) 
  16.              { 
  17.                  testDictioary.Add(temp, null); 
  18.              } 
  19.  
  20.              flag = testDictioary.Count; 
  21.  
  22.          } while (flag < 20); 
  23.  
  24.          Console.WriteLine("未排序前:"); 
  25.  
  26.          foreach (int key in testDictioary.Keys) 
  27.          { 
  28.              Console.Write(string.Format(@"{0}  ", key)); 
  29.          } 
  30.  
  31.          Console.WriteLine(); 
  32.  
  33.          CustomMethod.Sort(testDictioary); 
  34.  
  35.          Console.WriteLine("排序后:"); 
  36.  
  37.          foreach (int key in testDictioary.Keys) 
  38.          { 
  39.              Console.Write(string.Format(@"{0}  ", key)); 
  40.          } 
  41.  
  42.          Console.ReadLine(); 
  43.  
  44.      } 


那么这种方法是否是多此一举呢,因为SortedDictionary完全可以满足排序了。但是有时key会是这样的,100+“ssdd”,或100+20,是字符串类型。这样就能用到上面的方法了。比如以key为100+20这种类型为例。改动一下Sort方法。

  1. public static void Sort(Dictionary<string, object> dictionary) 
  2.      { 
  3.          try 
  4.          { 
  5.              List<int> sortList = new List<int>(); 
  6.  
  7.              Dictionary<string, object> tempDictionary = new Dictionary<string, object>(); 
  8.  
  9.              foreach (string key in dictionary.Keys) 
  10.              { 
  11.                  int intKey = Convert.ToInt32(key.Substring(0, key.IndexOf("+"))); 
  12.  
  13.                  sortList.Add(intKey); 
  14.  
  15.                  tempDictionary.Add(key, dictionary[key]); 
  16.              } 
  17.  
  18.              int flag = 1; 
  19.  
  20.              int i, j; 
  21.  
  22.              int itemCount = sortList.Count; 
  23.  
  24.              int itemTemp; 
  25.  
  26.              for (i = 1; i < itemCount && flag == 1; i++) 
  27.              { 
  28.                  flag = 0; 
  29.  
  30.                  for (j = 0; j < itemCount - i; j++) 
  31.                  { 
  32.                      int countfore = sortList[j]; 
  33.  
  34.                      int countback = sortList[j + 1]; 
  35.  
  36.                      if (countfore > countback) 
  37.                      { 
  38.                          flag = 1; 
  39.  
  40.                          itemTemp = sortList[j]; 
  41.  
  42.                          sortList[j] = sortList[j + 1]; 
  43.  
  44.                          sortList[j + 1] = itemTemp; 
  45.                      } 
  46.                  } 
  47.              } 
  48.              dictionary.Clear(); 
  49.  
  50.              for (int n = 0; n < itemCount; n++) 
  51.              { 
  52.                  foreach (string tempKey in tempDictionary.Keys) 
  53.                  { 
  54.                      string value = sortList[n].ToString(); 
  55.  
  56.                      if (tempKey.StartsWith(string.Format(@"{0}+", value))) 
  57.                      { 
  58.                          if (!dictionary.ContainsKey(tempKey)) 
  59.                          { 
  60.                              dictionary.Add(tempKey, tempDictionary[tempKey]); 
  61.                          } 
  62.                      } 
  63.                  } 
  64.              } 
  65.          } 
  66.          catch { } 
  67.      } 

        调用:

  1. static void Main(string[] args) 
  2.     Console.WriteLine("key为字符串"); 
  3.  
  4.     Dictionary<string, object> testDictioary = new Dictionary<string, object>(); 
  5.  
  6.     int flag = 0; 
  7.  
  8.     do 
  9.     { 
  10.         Random random = new Random(); 
  11.  
  12.         int temp = random.Next(100); 
  13.  
  14.         int tempValue = random.Next(100); 
  15.  
  16.         string keyString = string.Format(@"{0}+{1}", temp, tempValue); 
  17.  
  18.         if (!testDictioary.ContainsKey(keyString)) 
  19.         { 
  20.             testDictioary.Add(keyString, null); 
  21.         } 
  22.  
  23.         flag = testDictioary.Count; 
  24.  
  25.     } while (flag < 20); 
  26.  
  27.     Console.WriteLine("未排序前:"); 
  28.  
  29.     foreach (string key in testDictioary.Keys) 
  30.     { 
  31.         Console.Write(string.Format(@"{0}  ", key)); 
  32.     } 
  33.  
  34.     Console.WriteLine(); 
  35.  
  36.     CustomOtherSort.Sort(testDictioary); 
  37.  
  38.     Console.WriteLine("排序后:"); 
  39.  
  40.     foreach (string key in testDictioary.Keys) 
  41.     { 
  42.         Console.Write(string.Format(@"{0}  ", key)); 
  43.     } 
  44.  
  45.     Console.ReadLine(); 
  46.  

        结果:
       
        详细工程:http://download.csdn.net/detail/yysyangyangyangshan/4114133

目录
相关文章
|
运维 监控 NoSQL
【MongoDB 复制集秘籍】Secondary 同步慢怎么办?深度解析与实战指南,让你的数据库飞速同步!
【8月更文挑战第24天】本文通过一个具体案例探讨了MongoDB复制集中Secondary成员同步缓慢的问题。现象表现为数据延迟增加,影响业务运行。经分析,可能的原因包括硬件资源不足、网络状况不佳、复制日志错误等。解决策略涵盖优化硬件(如增加内存、升级CPU)、调整网络配置以减少延迟以及优化MongoDB配置(例如调整`oplogSize`、启用压缩)。通过这些方法可有效提升同步效率,保证系统的稳定性和性能。
403 4
|
9月前
|
机器学习/深度学习 数据可视化 计算机视觉
YOLOv11改进策略【注意力机制篇】| 2024 SCSA-CBAM 空间和通道的协同注意模块
YOLOv11改进策略【注意力机制篇】| 2024 SCSA-CBAM 空间和通道的协同注意模块
971 2
YOLOv11改进策略【注意力机制篇】| 2024 SCSA-CBAM 空间和通道的协同注意模块
|
12月前
|
传感器 数据采集 人工智能
数字孪生与环境监测:生态保护的新手段
数字孪生技术通过传感器、物联网等手段,实现对物理环境的实时监测、预测与优化,助力环境监测和生态保护。本文探讨其在空气质量、水质监测及生态系统管理中的应用,并展望未来挑战与前景。
|
存储 设计模式 编解码
.NET 8.0 通用管理平台,支持模块化、WinForms 和 WPF
【11月更文挑战第5天】本文分析了.NET 8.0 通用管理平台在模块化、WinForms 和 WPF 方面的优势。模块化设计提升了系统的可维护性和可扩展性,提高了代码复用性;WinForms 提供了丰富的控件库和简单易用的开发模式,技术成熟稳定;WPF 支持强大的数据绑定和 MVVM 模式,具备丰富的图形和动画功能,以及灵活的布局系统。
582 2
|
vr&ar 网络架构
配置BGP的基本功能
本文介绍了在AR1、AR2、AR3和AR4路由器之间配置BGP协议的过程。具体包括:在AR1和AR2之间建立EBGP连接,在AR2、AR3和AR4之间建立IBGP全连接,配置IP地址,发布AR1的LoopBack1端口,引入直连路由,并通过ping测试验证网络连通性。
配置BGP的基本功能
LiveCharts 直方图详解,安装和使用,以及常用属性的说明
本文介绍了LiveCharts在WPF中的应用,包括安装方法、基本使用和直方图(LineSeries)的常用属性说明。安装LiveCharts通过NuGet包管理器进行,使用时需在XAML文件中引入相应的命名空间。文章还提供了直方图的属性详解和综合示例,包括线条样式、坐标轴标签、图例位置等设置,以及如何自定义数据点形状。
LiveCharts 直方图详解,安装和使用,以及常用属性的说明
|
XML JSON Kubernetes
什么是 YAML?:一种简洁高效的数据序列化格式
什么是 YAML?:一种简洁高效的数据序列化格式
1783 0
|
NoSQL Java 数据库连接
《滚雪球学Spring Boot》教程导航帖(更新于2024.07.16)
📚 《滚雪球学Spring Boot》是由CSDN博主bug菌创作的全面Spring Boot教程。作者是全栈开发专家,在多个技术社区如CSDN、掘金、InfoQ、51CTO等担任博客专家,并拥有超过20万的全网粉丝。该教程分为入门篇和进阶篇,每篇包含详细的教学步骤,涵盖Spring Boot的基础和高级主题。
919 1
《滚雪球学Spring Boot》教程导航帖(更新于2024.07.16)
|
存储 缓存 前端开发
Servlet与JSP在Java Web应用中的性能调优策略
Servlet与JSP在Java Web应用中的性能调优策略
195 1
|
存储 开发框架 安全
【Uniapp 专栏】探索 Uniapp 的本地存储功能特点
【5月更文挑战第14天】Uniapp的本地存储功能是其跨平台开发的强大优势,提供数据持久化的存储、读取,支持多种数据类型。其特点是简单易用、跨平台一致且有一定安全性。通过键值对存储和容量管理,适应不同应用场景,如用户登录信息存储和应用配置保存。同时,注意性能优化、数据清理和安全保护。与其他框架比较,Uniapp有独特优势,并将持续发展以满足复杂需求。开发者应根据业务需求设计存储策略,确保数据安全高效使用。
537 2
【Uniapp 专栏】探索 Uniapp 的本地存储功能特点