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

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,104 questions

40,777 answers

573 users

How to sort a dictionary by value in C#

7 Answers

0 votes
using System;
using System.Linq;
using System.Collections.Generic;
 
class Program 
{
    static void Main(string[] args) {
        Dictionary<string, int> dic = new Dictionary<string, int>();
 
        dic.Add("c#", 99);
        dic.Add("php", 44);
        dic.Add("c++", 888);
        dic.Add("java", 1);
 
        List<KeyValuePair<string, int>> list = dic.ToList();
 
        list.Sort((pair1, pair2) => pair1.Value.CompareTo(pair2.Value));
 
        foreach (var element in list)
            Console.WriteLine(element);
    }
}
 
 
 
 
/*
run:
      
[java, 1]
[php, 44]
[c#, 99]
[c++, 888]
  
*/

 





answered Feb 20, 2017 by avibootz
edited Mar 18, 2023 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic;
 
class Program
{
    static void Main(string[] args) {
        Dictionary<string, int> dic = new Dictionary<string, int>();
 
        dic.Add("c#", 99);
        dic.Add("php", 44);
        dic.Add("c++", 888);
        dic.Add("java", 1);
 
        List<KeyValuePair<string, int>> list = dic.ToList();
 
        list.Sort((pair1, pair2) => pair1.Value.CompareTo(pair2.Value));
 
        foreach (KeyValuePair<string, int> kv in list)
            Console.WriteLine(kv.Key + " - " + kv.Value);
    }
}
 
 
 
/*
run:
      
java - 1
php - 44
c# - 99
c++ - 888
  
*/

 





answered Feb 20, 2017 by avibootz
edited Mar 18, 2023 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic;
 
class Program
{
    static void Main(string[] args) {
        Dictionary<string, int> dic = new Dictionary<string, int>();
 
        dic.Add("c#", 99);
        dic.Add("php", 44);
        dic.Add("c++", 888);
        dic.Add("java", 1);
 
        var sortedDic = from entry in dic orderby entry.Value ascending select entry;
 
        foreach (KeyValuePair<string, int> keyval in sortedDic) {
                Console.WriteLine("Key = {0}, Value = {1}", keyval.Key, keyval.Value);
        }
    }
}
 
 
 
 
/*
run:
      
Key = java, Value = 1
Key = php, Value = 44
Key = c#, Value = 99
Key = c++, Value = 888
  
*/

 





answered Feb 20, 2017 by avibootz
edited Mar 18, 2023 by avibootz
0 votes
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();

            dic.Add("c#", 99);
            dic.Add("php", 44);
            dic.Add("c++", 888);
            dic.Add("java", 1);

            var sortedDic = from entry in dic orderby entry.Value descending select entry;

            foreach (KeyValuePair<string, int> keyval in sortedDic)
                Console.WriteLine("Key = {0}, Value = {1}", keyval.Key, keyval.Value);
        }
    }
}


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

 





answered Feb 20, 2017 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic;
 
class Program
{
    static void Main(string[] args) {
        Dictionary<string, int> dic = new Dictionary<string, int>();
 
        dic.Add("c#", 99);
        dic.Add("php", 44);
        dic.Add("c++", 888);
        dic.Add("java", 1);
 
        foreach (KeyValuePair<string, int> keyval in dic.OrderBy(key => key.Value)) {
            Console.WriteLine("Key = {0}, Value = {1}", keyval.Key, keyval.Value);
        }
    }
}
 
 
 
/*
run:
      
Key = java, Value = 1
Key = php, Value = 44
Key = c#, Value = 99
Key = c++, Value = 888
  
*/

 





answered Feb 20, 2017 by avibootz
edited Mar 18, 2023 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args) {
        Dictionary<string, int> dic = new Dictionary<string, int>();

        dic.Add("c#", 99);
        dic.Add("php", 44);
        dic.Add("c++", 888);
        dic.Add("java", 1);

        var sortedDic = dic.OrderBy(x => x.Value);

        foreach (KeyValuePair<string, int> keyval in sortedDic) {
                Console.WriteLine("Key = {0}, Value = {1}", keyval.Key, keyval.Value);
        }
    }
}




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

 





answered Feb 20, 2017 by avibootz
edited Mar 18, 2023 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args) {
        Dictionary<string, int> dic = new Dictionary<string, int>();

        dic.Add("c#", 99);
        dic.Add("php", 44);
        dic.Add("c++", 888);
        dic.Add("java", 1);

        var list = dic.Keys.ToList();
        list.Sort();

        foreach (var key in list) {
            Console.WriteLine("{0} - {1}", key, dic[key]);
        }
    }
}




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

 





answered Feb 21, 2017 by avibootz
edited Mar 18, 2023 by avibootz

Related questions

1 answer 115 views
1 answer 98 views
98 views asked Oct 17, 2018 by avibootz
1 answer 37 views
1 answer 32 views
6 answers 147 views
147 views asked Mar 18, 2023 by avibootz
...