天天看點

Dictionary排序

有時候由于某些要求會對Dictionary排序,一般有兩種方法。

1、使用SortedDictionary。

這種自動會對儲存的值進行排序。

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)); 

           } 

結果:

Dictionary排序

2、自己寫的方法。如下

public static

void Sort(Dictionary<int,

object> dictionary) 

       { 

           try 

               List<int> sortList =

new List<int>(); 

               Dictionary<int,

object> tempDictionary = new Dictionary<int,

               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 { } 

       } 

        調用結果如下:

     { 

         Console.WriteLine("key為數字"); 

         Dictionary<int,

object> testDictioary = new Dictionary<int,

         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

             Console.Write(string.Format(@"{0}  ", key)); 

         } 

         Console.WriteLine(); 

         CustomMethod.Sort(testDictioary); 

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

         Console.ReadLine(); 

     } 

Dictionary排序

那麼這種方法是否是多此一舉呢,因為SortedDictionary完全可以滿足排序了。但是有時key會是這樣的,100+“ssdd”,或100+20,是字元串類型。這樣就能用到上面的方法了。比如以key為100+20這種類型為例。改動一下Sort方法。

void Sort(Dictionary<string,

         try 

             List<int> sortList =

             Dictionary<string,

object> tempDictionary = new Dictionary<string,

             foreach (string key

                 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

                     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為字元串"); 

    Dictionary<string,

object> testDictioary = new Dictionary<string,

    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,

        } 

        flag = testDictioary.Count; 

    } while (flag < 20); 

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

    foreach (string key

        Console.Write(string.Format(@"{0}  ", key)); 

    } 

    Console.WriteLine(); 

    CustomOtherSort.Sort(testDictioary); 

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

    Console.ReadLine(); 

        結果:

Dictionary排序