Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to sort a dictionary by value in VB.NET

6 Answers

0 votes
Imports System
Imports System.Linq
Imports System.Collections.Generic

Public Class Program
	Public Shared Sub Main(ByVal args As String())
        Dim dic As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)()
		
        dic.Add("c#", 99)
        dic.Add("php", 44)
        dic.Add("c++", 888)
        dic.Add("java", 1)
		
        Dim list As List(Of KeyValuePair(Of String, Integer)) = dic.ToList()
        
		list.Sort(Function(pair1, pair2) pair1.Value.CompareTo(pair2.Value))

        For Each element In list
            Console.WriteLine(element)
        Next
    End Sub
End Class




' run:
'
' [java, 1]
' [php, 44]
' [c#, 99]
' [c++, 888]
'

 



answered Mar 18, 2023 by avibootz
0 votes
Imports System
Imports System.Linq
Imports System.Collections.Generic

Public Class Program
	Public Shared Sub Main(ByVal args As String())
        Dim dic As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)()
		
        dic.Add("c#", 99)
        dic.Add("php", 44)
        dic.Add("c++", 888)
        dic.Add("java", 1)
		
        Dim list As List(Of KeyValuePair(Of String, Integer)) = dic.ToList()
		
        list.Sort(Function(pair1, pair2) pair1.Value.CompareTo(pair2.Value))

        For Each kv As KeyValuePair(Of String, Integer) In list
            Console.WriteLine(kv.Key & " - " & kv.Value)
        Next
    End Sub
End Class





' run:
'
' java - 1
' php - 44
' c# - 99
' c++ - 888
'

 



answered Mar 18, 2023 by avibootz
0 votes
Imports System
Imports System.Linq
Imports System.Collections.Generic

Public Class Program
	Public Shared Sub Main(ByVal args As String())
        Dim dic As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)()
		
        dic.Add("c#", 99)
        dic.Add("php", 44)
        dic.Add("c++", 888)
        dic.Add("java", 1)
		
        Dim sortedDic = From entry In dic Order By entry.Value Select entry

        For Each keyval As KeyValuePair(Of String, Integer) In sortedDic
            Console.WriteLine("Key = {0}, Value = {1}", keyval.Key, keyval.Value)
        Next
    End Sub
End Class






' run:
'
' Key = java, Value = 1
' Key = php, Value = 44
' Key = c#, Value = 99
' Key = c++, Value = 888
'

 



answered Mar 18, 2023 by avibootz
0 votes
Imports System
Imports System.Linq
Imports System.Collections.Generic

Public Class Program
	Public Shared Sub Main(ByVal args As String())
        Dim dic As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)()
		
        dic.Add("c#", 99)
        dic.Add("php", 44)
        dic.Add("c++", 888)
        dic.Add("java", 1)

        For Each keyval As KeyValuePair(Of String, Integer) In dic.OrderBy(Function(key) key.Value)
            Console.WriteLine("Key = {0}, Value = {1}", keyval.Key, keyval.Value)
        Next
    End Sub
End Class







' run:
'
' Key = java, Value = 1
' Key = php, Value = 44
' Key = c#, Value = 99
' Key = c++, Value = 888
'

 



answered Mar 18, 2023 by avibootz
0 votes
Imports System
Imports System.Linq
Imports System.Collections.Generic

Public Class Program
	Public Shared Sub Main(ByVal args As String())
        Dim dic As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)()
		
        dic.Add("c#", 99)
        dic.Add("php", 44)
        dic.Add("c++", 888)
        dic.Add("java", 1)
        
		Dim sortedDic = dic.OrderBy(Function(x) x.Value)

        For Each keyval As KeyValuePair(Of String, Integer) In sortedDic
            Console.WriteLine("Key = {0}, Value = {1}", keyval.Key, keyval.Value)
        Next
    End Sub
End Class







' run:
'
' Key = java, Value = 1
' Key = php, Value = 44
' Key = c#, Value = 99
' Key = c++, Value = 888
'

 



answered Mar 18, 2023 by avibootz
0 votes
Imports System
Imports System.Linq
Imports System.Collections.Generic

Public Class Program
	Public Shared Sub Main(ByVal args As String())
        Dim dic As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)()
		
        dic.Add("c#", 99)
        dic.Add("php", 44)
        dic.Add("c++", 888)
        dic.Add("java", 1)
		
        Dim list = dic.Keys.ToList()
		
        list.Sort()

        For Each key In list
            Console.WriteLine("{0} - {1}", key, dic(key))
        Next
    End Sub
End Class






' run:
'
' c# - 99
' c++ - 888
' java - 1
' php - 44
'

 



answered Mar 18, 2023 by avibootz

Related questions

...