Sorting Dictionary by Values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Pöpping

    Sorting Dictionary by Values

    Hello,

    I´ve a simple question about existing data structures/ collections.

    Is there a way to sort a dictionary (or any comparable collection/ data
    structure) by values instead of by keys?

    For example if you have a dictionary with words as keys and the
    frequency of these words in a text as values.

    What is the most performant way to sort these dictionary by the
    frequency of the words (=values)?



    Regards,

    Martin
  • John Timney \(MVP\)

    #2
    Re: Sorting Dictionary by Values

    have you looked at the generic SortedDictionar y class



    --
    --
    Regards

    John Timney (MVP)
    VISIT MY WEBSITE:




    "Martin Pöpping" <martin_p@despa mmed.comwrote in message
    news:ejcgev$ddg $1@newsreader2. netcologne.de.. .
    Hello,
    >
    I´ve a simple question about existing data structures/ collections.
    >
    Is there a way to sort a dictionary (or any comparable collection/ data
    structure) by values instead of by keys?
    >
    For example if you have a dictionary with words as keys and the frequency
    of these words in a text as values.
    >
    What is the most performant way to sort these dictionary by the frequency
    of the words (=values)?
    >
    >
    >
    Regards,
    >
    Martin

    Comment

    • Bill Block

      #3
      Re: Sorting Dictionary by Values

      Two options:

      1) Use a List<Timpelemen tation to store your data objects, then use
      the Sort() method. For example:

      List<MyClassm_l ist = new List<MyClass>() ;
      m_list.Add(inst ance1);
      m_list.Add(inst ance2);
      m_list.Sort({Ge neric Comparison});

      Sorts the elements or a portion of the elements in the List&lt;T&gt; using either the specified or default IComparer&lt;T&gt; implementation or a provided Comparison&lt;T&gt; delegate to compare list elements.


      2) Implement a custom sortable binding list implementation (if you need
      binding as well). There is a good article on creating a
      SortableBinding List in MSDN:




      Bill

      Comment

      • Martin Pöpping

        #4
        Re: Sorting Dictionary by Values

        John Timney (MVP) schrieb:
        have you looked at the generic SortedDictionar y class
        >
        http://msdn2.microsoft.com/en-us/library/f7fta44c.aspx
        Sure, but the SortedDictionar y sorts by Key and I want to sort
        everything by value

        Comment

        • John Timney \(MVP\)

          #5
          Re: Sorting Dictionary by Values

          oops - so it does.

          Heres some code to sort the contents of the dictionary based on its values,
          having key and vlaues both of type string. See if you can work with that.

          Dictionary<stri ng, strings = new Dictionary<stri ng, string>();
          s.Add("1", "a Item");
          s.Add("2", "c Item");
          s.Add("3", "b Item");

          List<KeyValuePa ir<string, string>myList = new
          List<KeyValuePa ir<string, string>>(s);
          myList.Sort(
          delegate(KeyVal uePair<string, stringfirstPair ,
          KeyValuePair<st ring, stringnextPair)
          {
          return firstPair.Value .CompareTo(next Pair.Value);
          }
          );

          foreach (KeyValuePair<s tring, stringmyKey in myList)
          {
          Response.Write( myKey.Key + " " + myKey.Value);
          }


          --
          Regards

          John Timney (MVP)
          VISIT MY WEBSITE:




          "Martin Pöpping" <martin_p@despa mmed.comwrote in message
          news:ejcrdn$1qr $1@newsreader2. netcologne.de.. .
          John Timney (MVP) schrieb:
          >have you looked at the generic SortedDictionar y class
          >>
          >http://msdn2.microsoft.com/en-us/library/f7fta44c.aspx
          >
          Sure, but the SortedDictionar y sorts by Key and I want to sort everything
          by value

          Comment

          • Tony Gravagno

            #6
            Re: Sorting Dictionary by Values

            This article by Eric Gunnerson at Microsoft was very helpful for
            creating a sorted, enumerable hash table:

            I made a minor tweak to the IterSortHashVal ue code to return an Object
            which can be used like a Hashtable for DropDownLists which exposes
            both a Key and a Value.

            I haven't compared any of these techniques and don't know if the OP
            really needs to have a Dictionary or if that's just what he happens to
            be looking at, at the moment.

            HTH


            "John Timney \(MVP\)" wrote:
            >oops - so it does.
            >
            >Heres some code to sort the contents of the dictionary based on its values,
            >having key and vlaues both of type string. See if you can work with that.
            >
            Dictionary<stri ng, strings = new Dictionary<stri ng, string>();
            s.Add("1", "a Item");
            s.Add("2", "c Item");
            s.Add("3", "b Item");
            >
            List<KeyValuePa ir<string, string>myList = new
            >List<KeyValueP air<string, string>>(s);
            myList.Sort(
            delegate(KeyVal uePair<string, stringfirstPair ,
            >KeyValuePair<s tring, stringnextPair)
            {
            return firstPair.Value .CompareTo(next Pair.Value);
            }
            );
            >
            foreach (KeyValuePair<s tring, stringmyKey in myList)
            {
            Response.Write( myKey.Key + " " + myKey.Value);
            }

            Comment

            Working...