How do you sort a dictionary by value?

前端 未结 19 2651
误落风尘
误落风尘 2020-11-22 03:51

I often have to sort a dictionary, consisting of keys & values, by value. For example, I have a hash of words and respective frequencies, that I want to order by frequen

19条回答
  •  花落未央
    2020-11-22 04:06

    The easiest way to get a sorted Dictionary is to use the built in SortedDictionary class:

    //Sorts sections according to the key value stored on "sections" unsorted dictionary, which is passed as a constructor argument
    System.Collections.Generic.SortedDictionary sortedSections = null;
    if (sections != null)
    {
        sortedSections = new SortedDictionary(sections);
    }
    

    sortedSections will contain the sorted version of sections

提交回复
热议问题