有时候由于某些要求会对Dictionary排序,一般有两种方法。

1、使用SortedDictionary。

这种自动会对保存的值进行排序。

  1. static void Main(string[] args)
  2. {
  3. SortedDictionary<int,
    object> testDictioary = new SortedDictionary<int,
    object>();
  4. int flag = 0;
  5. do
  6. {
  7. Random random = new Random();
  8. int temp = random.Next(100);
  9. if (!testDictioary.ContainsKey(temp))
  10. {
  11. testDictioary.Add(temp, null);
  12. }
  13. flag = testDictioary.Count;
  14. } while (flag < 20);
  15. Console.WriteLine("未排序前:");
  16. foreach (int key
    in testDictioary.Keys)
  17. {
  18. Console.Write(string.Format(@"{0}  ", key));
  19. }
 static void Main(string[] args)
 {
      SortedDictionary<int, object> testDictioary = new SortedDictionary<int, object>();
            int flag = 0;

            do
            {
                Random random = new Random();

                int temp = random.Next(100);

                if (!testDictioary.ContainsKey(temp))
                {
                    testDictioary.Add(temp, null);
                }

                flag = testDictioary.Count;

            } while (flag < 20);

            Console.WriteLine("未排序前:");

            foreach (int key in testDictioary.Keys)
            {
                Console.Write(string.Format(@"{0}  ", key));
            }
}

结果:

2、自己写的方法。如下

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

                Dictionary<int, object> tempDictionary = new Dictionary<int, object>();

                foreach (int key in dictionary.Keys)
                {
                    sortList.Add(key);

                    tempDictionary.Add(key, dictionary[key]);
                }

                int flag = 1;

                int i, j;

                int itemCount = sortList.Count;

                int itemTemp;

                for (i = 1; i < itemCount && flag == 1; i++)
                {
                    flag = 0;

                    for (j = 0; j < itemCount - i; j++)
                    {
                        int countfore = sortList[j];

                        int countback = sortList[j + 1];

                        if (countfore > countback)
                        {
                            flag = 1;

                            itemTemp = sortList[j];

                            sortList[j] = sortList[j + 1];

                            sortList[j + 1] = itemTemp;
                        }
                    }
                }
                dictionary.Clear();

                for (int n = 0; n < itemCount; n++)
                {
                    foreach (int tempKey in tempDictionary.Keys)
                    {
                        int value = sortList[n];

                        if (tempKey.Equals(value))
                        {
                            if (!dictionary.ContainsKey(tempKey))
                            {
                                dictionary.Add(tempKey, tempDictionary[tempKey]);
                            }
                        }
                    }
                }
            }
            catch { }
        }

调用结果如下:

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

            Dictionary<int, object> testDictioary = new Dictionary<int, object>();

            int flag = 0;

            do
            {
                Random random = new Random();

                int temp = random.Next(100);

                if (!testDictioary.ContainsKey(temp))
                {
                    testDictioary.Add(temp, null);
                }

                flag = testDictioary.Count;

            } while (flag < 20);

            Console.WriteLine("未排序前:");

            foreach (int key in testDictioary.Keys)
            {
                Console.Write(string.Format(@"{0}  ", key));
            }

            Console.WriteLine();

            CustomMethod.Sort(testDictioary);

            Console.WriteLine("排序后:");

            foreach (int key in testDictioary.Keys)
            {
                Console.Write(string.Format(@"{0}  ", key));
            }

            Console.ReadLine();

        }



那么这种方法是否是多此一举呢,因为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. Dictionary<string,
    object> tempDictionary = new Dictionary<string,
    object>();
  7. foreach (string key
    in dictionary.Keys)
  8. {
  9. int intKey = Convert.ToInt32(key.Substring(0, key.IndexOf("+")));
  10. sortList.Add(intKey);
  11. tempDictionary.Add(key, dictionary[key]);
  12. }
  13. int flag = 1;
  14. int i, j;
  15. int itemCount = sortList.Count;
  16. int itemTemp;
  17. for (i = 1; i < itemCount && flag == 1; i++)
  18. {
  19. flag = 0;
  20. for (j = 0; j < itemCount - i; j++)
  21. {
  22. int countfore = sortList[j];
  23. int countback = sortList[j + 1];
  24. if (countfore > countback)
  25. {
  26. flag = 1;
  27. itemTemp = sortList[j];
  28. sortList[j] = sortList[j + 1];
  29. sortList[j + 1] = itemTemp;
  30. }
  31. }
  32. }
  33. dictionary.Clear();
  34. for (int n = 0; n < itemCount; n++)
  35. {
  36. foreach (string tempKey
    in tempDictionary.Keys)
  37. {
  38. string value = sortList[n].ToString();
  39. if (tempKey.StartsWith(string.Format(@"{0}+", value)))
  40. {
  41. if (!dictionary.ContainsKey(tempKey))
  42. {
  43. dictionary.Add(tempKey, tempDictionary[tempKey]);
  44. }
  45. }
  46. }
  47. }
  48. }
  49. catch { }
  50. }
   public static void Sort(Dictionary<string, object> dictionary)
        {
            try
            {
                List<int> sortList = new List<int>();

                Dictionary<string, object> tempDictionary = new Dictionary<string, object>();

                foreach (string key in dictionary.Keys)
                {
                    int intKey = Convert.ToInt32(key.Substring(0, key.IndexOf("+")));

                    sortList.Add(intKey);

                    tempDictionary.Add(key, dictionary[key]);
                }

                int flag = 1;

                int i, j;

                int itemCount = sortList.Count;

                int itemTemp;

                for (i = 1; i < itemCount && flag == 1; i++)
                {
                    flag = 0;

                    for (j = 0; j < itemCount - i; j++)
                    {
                        int countfore = sortList[j];

                        int countback = sortList[j + 1];

                        if (countfore > countback)
                        {
                            flag = 1;

                            itemTemp = sortList[j];

                            sortList[j] = sortList[j + 1];

                            sortList[j + 1] = itemTemp;
                        }
                    }
                }
                dictionary.Clear();

                for (int n = 0; n < itemCount; n++)
                {
                    foreach (string tempKey in tempDictionary.Keys)
                    {
                        string value = sortList[n].ToString();

                        if (tempKey.StartsWith(string.Format(@"{0}+", value)))
                        {
                            if (!dictionary.ContainsKey(tempKey))
                            {
                                dictionary.Add(tempKey, tempDictionary[tempKey]);
                            }
                        }
                    }
                }
            }
            catch { }
        }

调用:

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

            Dictionary<string, object> testDictioary = new Dictionary<string, object>();

            int flag = 0;

            do
            {
                Random random = new Random();

                int temp = random.Next(100);

                int tempValue = random.Next(100);

                string keyString = string.Format(@"{0}+{1}", temp, tempValue);

                if (!testDictioary.ContainsKey(keyString))
                {
                    testDictioary.Add(keyString, null);
                }

                flag = testDictioary.Count;

            } while (flag < 20);

            Console.WriteLine("未排序前:");

            foreach (string key in testDictioary.Keys)
            {
                Console.Write(string.Format(@"{0}  ", key));
            }

            Console.WriteLine();

            CustomOtherSort.Sort(testDictioary);

            Console.WriteLine("排序后:");

            foreach (string key in testDictioary.Keys)
            {
                Console.Write(string.Format(@"{0}  ", key));
            }

            Console.ReadLine();

        }

结果:

       

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

Dictionary排序的更多相关文章

  1. C#语法基础用法Dictionary排序

    Dictionary排序 1.先看效果图: 2.核心逻辑如下: Dictionary<int, string> list = new Dictionary<int, string&g ...

  2. C#字典Dictionary排序(顺序、倒序)

    这里是针对.NET版本过低的排序方式,没怎么用过,记录一下: 一.创建字典Dictionary 对象 假如 Dictionary 中保存的是一个网站页面流量,key 是网页名称,值value对应的是网 ...

  3. Linq list 排序,Dictionary 排序

    C# 对List成员排序的简单方法 http://blog.csdn.net/wanzhuan2010/article/details/6205884 LINQ之路系列博客导航 http://www. ...

  4. C#中Dictionary排序方式

    转载自:https://www.cnblogs.com/5696-an/p/5625142.html 自定义类: https://files.cnblogs.com/files/xunhanliu/d ...

  5. C#中Dictionary<TKey,TValue>排序方式

    自定义类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sy ...

  6. c# Dictionary的遍历和排序

    c# Dictionary的遍历和排序 c#遍历的两种方式 for和foreach for: 需要指定首位数据.末尾数据.数据长度: for遍历语句中可以改变数据的值: 遍历规则可以自定义,灵活性较高 ...

  7. c# Dictionary的遍历和排序(转)

    c#遍历的两种方式 for和foreach for: 需要指定首位数据.末尾数据.数据长度: for遍历语句中可以改变数据的值: 遍历规则可以自定义,灵活性较高 foreach: 需要实现ienume ...

  8. C#对 Dictionary进行排序 转

    C# .net 3.5 以上的版本引入 Linq 后,字典Dictionary排序变得十分简单,用一句类似 sql 数据库查询语句即可搞定:不过,.net 2.0 排序要稍微麻烦一点,为便于使用,将总 ...

  9. c# 关于字典dictionary 按时间排序

    上文中说到sortedlist 排序是键排序,不符合项目要求问题,接着使用字典dictionary 对value 为时间按照升序排序,问题解决.中间涉及到linq的使用.还有其他的写法,但发现下边的写 ...

随机推荐

  1. Linux(CentOS)挂载NTFS格式的U盘、移动硬盘

    以下操作均在root下执行的 1.U盘挂载 mkdir /mnt/usb //创建一个目录,用于挂载U盘 fdisk -l //查看系统中挂载的U盘,若系统有一块硬盘sdb1 代表你的U盘,/dev/ ...

  2. Nodejs的运行原理-模块篇

    前言 使用Nodejs,就不可避免地引用第三方模块,它们有些是Nodejs自带的(例:http,net...),有些是发布在npm上的(例:mssql,elasticsearch...) 本篇章聚焦3 ...

  3. 编译预处理命令define

    #include 包含指令 将一个源文件嵌入到当前源文件中该点处. #include<文件名>  按标准方式搜索,文件位于C++系统目录的include子目录下 #include" ...

  4. c语言环境初始化&c语言和汇编混合编程

    bootloader通常会分为两个阶段:第一阶段采用汇编语言来编写,主要是一些核心的初始化工作(内存,时钟的初始化),第二阶段使用C语言来编写,主要是它会完成一些板载硬件的初始化(串口,网口)然后其启 ...

  5. C语言头文件中定义全局变量导致重复定义错误

    合作方升级SDK后,程序编译出现变量重复定义的错误,通过错误提示无法找到什么位置重复定义了,但确定是引入新SDK后才出现的错误,从SDK的头文件中查找,最终发现在头文件中定义了全局变量 我们的项目在多 ...

  6. linux scp远程拷贝文件及文件夹

    [http://www.jb51.net/LINUXjishu/73131.html] 1.拷贝本机/home/administrator/test整个目录至远程主机192.168.1.100的/ro ...

  7. linux服务器ssh、公匙和密钥实战详解

    一..我们先建好一上haiwen用户用来,做为密码钥和SSH对像 二.修改vi /etc/ssh/sshd_config 文件,禁用ROOT远程直接登录. 三.ssh的公钥认证配置,只能用密匙才能登录 ...

  8. mysql之mysql_config_editor

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn mysql_config_editor允许你把登录的身份验证信息存储 ...

  9. 前端开发利器webStorm

    这里推荐一个前端开发工具webStorm.用了大概快半年了,发现所有其他工具无出其右的.目前最新版本已经到4.0.2,半年前还是2.X 相比aptana.dreamweaver.sublime和vim ...

  10. Linux系统上安装JDK1.8

    1,下载jdk1.8 首先进入jdk下载目录:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213315 ...