sort a dictionary by value in csharp

Code Example - sort a dictionary by value in csharp

                
                        var ordered = dict.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
                    
                
 

how to sort a dictionary by value in csharp

                        
                                Dictionary<string, int> myDict = new Dictionary<string, int>();
myDict.Add("one", 1);
myDict.Add("four", 4);
myDict.Add("two", 2);
myDict.Add("three", 3);

var sortedDict = from entry in myDict orderby entry.Value ascending select entry;
                            
                        
 

dictionary order by value csharp

                        
                                foreach (var item in dict.OrderBy(key=> key.Value))
{ 
    // do something with item.Key and item.Value
}