Avatar ·

How to sort a Dictionary by value in C#?

Hello everyone! I'm just starting to learn C# and encountered this problem: I have a `Dictionary` where the key is the student's name and the value is their exam score. I need to display the students in ascending order of scores, but I can't figure out how to do it. Here's an example of my code: ```csharp Dictionary scores = new Dictionary { { "Alice", 85 }, { "Bob", 92 }, { "Charlie", 78 }, { "Diana", 95 } }; // How to sort this dictionary by values (scores)? foreach (var pair in scores) { Console.WriteLine($"{pair.Key}: {pair.Value}"); } ``` I know that `Dictionary` itself doesn't support sorting because it's a hash table. But I need to get an ordered list of key-value pairs in ascending order.