有时候由于某些要求会对Dictionary排序,一般有两种方法。 1、使用SortedDictionary。 这种自动会对保存的值进行排序。
详细工程:
static void Main(string[] args) { SortedDictionarytestDictioary = new SortedDictionary (); 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、自己写的方法。如下
public static void Sort(Dictionarydictionary) { try { List sortList = new List (); Dictionary tempDictionary = new Dictionary (); 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 { } }
调用结果如下:
static void Main(string[] args) { Console.WriteLine("key为数字"); DictionarytestDictioary = new Dictionary (); 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(); }
public static void Sort(Dictionarydictionary) { try { List sortList = new List (); Dictionary tempDictionary = new Dictionary (); 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 { } }
调用:
static void Main(string[] args) { Console.WriteLine("key为字符串"); DictionarytestDictioary = new Dictionary (); 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://hi.csdn.net/attachment/201203/5/0_1330937146XL7y.gif)