代码如下 :
static void Main(string[] args) { Dictionarydi = new Dictionary (); di.Add(9, 1); di.Add(0, 6); di.Add(2, 8); var sortedDict = (from entry in di orderby entry.Key ascending select entry) .ToDictionary(pair => pair.Key, pair => pair.Value); foreach (var pair in di) { Console.WriteLine("{0},{1}", pair.Key, pair.Value); } Console.WriteLine("--------------------"); foreach (var pair in sortedDict) { Console.WriteLine("{0},{1}", pair.Key, pair.Value); } Console.ReadLine(); }
方法来自stackoverflow