How do you sort a dictionary by value?

前端 未结 19 2368
误落风尘
误落风尘 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 03:55
    var ordered = dict.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
    
    0 讨论(0)
  • 2020-11-22 03:55

    Sorting a SortedDictionary list to bind into a ListView control using VB.NET:

    Dim MyDictionary As SortedDictionary(Of String, MyDictionaryEntry)
    
    MyDictionaryListView.ItemsSource = MyDictionary.Values.OrderByDescending(Function(entry) entry.MyValue)
    
    Public Class MyDictionaryEntry ' Need Property for GridViewColumn DisplayMemberBinding
        Public Property MyString As String
        Public Property MyValue As Integer
    End Class
    

    XAML:

    <ListView Name="MyDictionaryListView">
        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Path=MyString}" Header="MyStringColumnName"></GridViewColumn>
                <GridViewColumn DisplayMemberBinding="{Binding Path=MyValue}" Header="MyValueColumnName"></GridViewColumn>
             </GridView>
        </ListView.View>
    </ListView>
    
    0 讨论(0)
  • 2020-11-22 03:56

    Use:

    using System.Linq.Enumerable;
    ...
    List<KeyValuePair<string, string>> myList = aDictionary.ToList();
    
    myList.Sort(
        delegate(KeyValuePair<string, string> pair1,
        KeyValuePair<string, string> pair2)
        {
            return pair1.Value.CompareTo(pair2.Value);
        }
    );
    

    Since you're targeting .NET 2.0 or above, you can simplify this into lambda syntax -- it's equivalent, but shorter. If you're targeting .NET 2.0 you can only use this syntax if you're using the compiler from Visual Studio 2008 (or above).

    var myList = aDictionary.ToList();
    
    myList.Sort((pair1,pair2) => pair1.Value.CompareTo(pair2.Value));
    
    0 讨论(0)
  • 2020-11-22 03:56

    Use LINQ:

    Dictionary<string, int> myDict = new Dictionary<string, int>();
    myDict.Add("one", 1);
    myDict.Add("four", 4);
    myDict.Add("two", 2);
    myDict.Add("three", 3);
    
    var sortedDict = from entry in myDict orderby entry.Value ascending select entry;
    

    This would also allow for great flexibility in that you can select the top 10, 20 10%, etc. Or if you are using your word frequency index for type-ahead, you could also include StartsWith clause as well.

    0 讨论(0)
  • 2020-11-22 03:56

    You can sort a Dictionary by value and save it back to itself (so that when you foreach over it the values come out in order):

    dict = dict.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
    

    Sure, it may not be correct, but it works.

    0 讨论(0)
  • 2020-11-22 03:57

    Suppose we have a dictionary as

       Dictionary<int, int> dict = new Dictionary<int, int>();
       dict.Add(21,1041);
       dict.Add(213, 1021);
       dict.Add(45, 1081);
       dict.Add(54, 1091);
       dict.Add(3425, 1061);
       sict.Add(768, 1011);
    

    1) you can use temporary dictionary to store values as :

            Dictionary<int, int> dctTemp = new Dictionary<int, int>();
    
            foreach (KeyValuePair<int, int> pair in dict.OrderBy(key => key.Value))
            {
                dctTemp .Add(pair.Key, pair.Value);
            }
    
    0 讨论(0)
提交回复
热议问题