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 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 187 views
1 answer 150 views
150 views asked Oct 17, 2018 by avibootz
1 answer 137 views
1 answer 134 views
6 answers 519 views
519 views asked Mar 18, 2023 by avibootz
1 answer 160 views
...