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 222 views
1 answer 191 views
191 views asked Oct 17, 2018 by avibootz
1 answer 167 views
1 answer 169 views
1 answer 180 views
...