Microsoft Developer Network > 포럼 홈 > .NET Base Class Library > C# 2.0: Sorting a Hashtable by Values
질문하기질문하기
 

답변됨C# 2.0: Sorting a Hashtable by Values

  • 2007년 6월 7일 목요일 오후 8:50_Bullines 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Hi!

    I have a hashtable in a .NET2.0 project such that:


    Code Snippet

    Hashtable myHT = new Hashtable();
    myHT.Add("Bob",  23);
    myHT.Add("Jane", 14);
    myHT.Add("Dave",  7);
    myHT.Add("Mike", 18);



    I would like to sort myHT by the values, and not the keys.  How would I go about doing that?  I've used IComparer before in .NET 1.1, but it appears to be obsolete in .NET2.0.  Can anybody offer some help?  Thanks in advance.



답변

  • 2007년 6월 8일 금요일 오후 10:05OmegaManMVP, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
     bruno_1 wrote:
    Dictionary<> is worst than Hashtable for sorting, because they don't even bother themself by putting the IComparer interface for comparing.


    Well yes and no, HashTable had to be the swiss army knife of operations. Dictionary is leaner. A dictionary is only for storing values with a key...there is no need to sort the whole thing...just its keys or values....

    The keys and values can be viewed as List<t>. The List<t> is created from the ICollection returned by the exposed properties on the Dictionary of Keys/Values.

    Here I extract the values, I could have done the keys, and sorted them.

    Code Snippet

    Dictionary<int, string> myDict = new Dictionary<int, string>();
    myDict.Add(2, "This");
    myDict.Add(1, "is");
    myDict.Add(5, "radio");
    myDict.Add(4, "clash");

    List<string> song = new List<string>(myDict.Values);
    song.Sort();

    // This writes out: "clash is radio This"
    Console.WriteLine(string.Join(" ", song.ToArray()));


  • 2007년 6월 9일 토요일 오후 8:04nobugzMVP, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    System.Collections.Generic.SortedDictionary would be the logical choice.  One of its constructors takes an IComparer<T> as its argument.  Yell if you need sample code.

모든 응답

  • 2007년 6월 7일 목요일 오후 10:00Bruno_1 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Just ignore or remove the warning, and implement IComparer, it is the simpler way to do it.I don't think that you can solve directly this with only using a hashtable without the IComparer.
  • 2007년 6월 7일 목요일 오후 11:52Andreas Johansson중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    You might want to have a look at Dictionary<T> as it is the hashed table to use in .NET 2.0.

     

    What was your approach to sort after values before?

  • 2007년 6월 8일 금요일 오전 5:34Bruno_1 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Dictionary<> is worst than Hashtable for sorting, because they don't even bother themself by putting the IComparer interface for comparing. Could you tell us how you sorted the hashtable by value before ?

  • 2007년 6월 8일 금요일 오후 10:05OmegaManMVP, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
     bruno_1 wrote:
    Dictionary<> is worst than Hashtable for sorting, because they don't even bother themself by putting the IComparer interface for comparing.


    Well yes and no, HashTable had to be the swiss army knife of operations. Dictionary is leaner. A dictionary is only for storing values with a key...there is no need to sort the whole thing...just its keys or values....

    The keys and values can be viewed as List<t>. The List<t> is created from the ICollection returned by the exposed properties on the Dictionary of Keys/Values.

    Here I extract the values, I could have done the keys, and sorted them.

    Code Snippet

    Dictionary<int, string> myDict = new Dictionary<int, string>();
    myDict.Add(2, "This");
    myDict.Add(1, "is");
    myDict.Add(5, "radio");
    myDict.Add(4, "clash");

    List<string> song = new List<string>(myDict.Values);
    song.Sort();

    // This writes out: "clash is radio This"
    Console.WriteLine(string.Join(" ", song.ToArray()));


  • 2007년 6월 9일 토요일 오후 8:04nobugzMVP, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    System.Collections.Generic.SortedDictionary would be the logical choice.  One of its constructors takes an IComparer<T> as its argument.  Yell if you need sample code.
  • 2007년 6월 13일 수요일 오후 8:34OmegaManMVP, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
     Chris Bellini wrote:
    I would like to sort myHT by the values, and not the keys. How would I go about doing that?


     nobugz wrote:
    System.Collections.Generic.SortedDictionary would be the logical choice. One of its constructors takes an IComparer<T> as its argument. Yell if you need sample code.


    Unless I am mistaken Hans, SortedDictionary only sorts on keys and not values:

    "Represents a collection of key/value pairs that are sorted on the key." - SortedDictionary Class MSDN
  • 2007년 6월 13일 수요일 오후 9:39Bruno_1 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    You're right this is why we asked the poster how he already did it with the hashtable, cause hashtable has similar functionality for the IComparer<T>, and it doesn't provide sorting by value.  And I think it shouldn't because the insertion is done by key(IComparer<T> is on the key). anyway, you can not sort in the same time by key and value.
    However, it is a good idea to mention the SortedDictionary<>, but i think that the solution can not be done just by one collection, there is also the SortedList<Tkey, TValue> wich is less efficient in inserting and removing, but much better for memory consumption than SortedDictionary.
  • 2007년 6월 13일 수요일 오후 11:49nobugzMVP, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    The Wintellect Power Collections have many OrderedSomething collections.  Something ought to fit your bill.
  • 2007년 12월 17일 월요일 오후 10:42MeMyselfAndI 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     제안된 답변

    I realize that is thread is 6 months old.  But, since I did not find a newer thread on the topic and I found a solution, I decided to post it.

     

    I had the same problem of needing to have a sorted pair of values.  I had used a hashtable to help build these values fas a summary of a very large collection.  I then needed to get the pairs with highest summary first.  I found the KeyValuePair in the Generics namespace to be very useful.  But to use KeyValuePair, I changed my hashtable to a dictionary.

     

    Dictionary<string, int> dict = new Dictionary<string, int>();

     

    /*  populate dictionary */

     

    List<KeyValuePair<string, int>> summaryList = new List<KeyValuePair<string, int>>();

     

    summaryList.AddRange(dict);

    summaryList.Sort

       (

          delegate(KeyValuePair<string, int> kvp1,

                       KeyValuePair<string, int> kvp2)

             {

                return Comparer<int>.Default.Compare(kvp1.Value, kvp2.Value);

             }

        );

     

    • 답변으로 제안됨MeMyselfAndI 2009년 1월 6일 화요일 오후 8:37
    •