按键和值对字典排序?

如果我想在C#中对字典进行排序,其顺序由其键及其值确定。 像它的值的降序和具有相同值的那些降序,按其键递减。 似乎很有可能只按键或仅按值排序,但两者都非常烦人。

using System.Linq; ... IOrderedEnumerable> sortedCollection = myDictionary .OrderByDescending(x => x.Value) .ThenByDescending(x => x.Key); 

如果您需要更复杂的排序方案,请实现IComparer>以在OrderBy或OrderByDescending的重载版本中使用。