How do you sort a dictionary by value?

前端 未结 19 2658
误落风尘
误落风尘 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

    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:

    
        
            
                
                
             
        
    
    

提交回复
热议问题