虽然这个问题有几个答案,但这些似乎都没有利用 SortedDictionary 结构。如果您希望将 SortedDictionary 设计为最大堆而不是默认的最小堆,我认为最好的解决方案是覆盖 C# 使用的默认比较器。这可以按如下方式完成:
public class DescendingComparer<T>: IComparer<T> where T : IComparable<T>
{
public int Compare(T x, T y)
{
return y.CompareTo(x); //reverses, so compare ascending
//this is vs the standard method, which returns x.CompareTo(y)
}
}
static void Main(string[] args)
{
SortedDictionary<float, string> myDict = new SortedDictionary<float,string>(new DescendingComparer<float>()); //sorts on the key
string[] name = {"Bill", "Tom", "Susan", "Terry"};
myDict.Add(.8f, name[0]);
myDict.Add(.2f, name[1]);
myDict.Add(.95f, name[2]);
myDict.Add(.005f, name[4]);
foreach (KeyValuePair<float, int> j in myDict)
{
Console.WriteLine("Key: {0}, Value: {1}",j.Key,j.Value);
} //now it is stored in increasing order, so accessing largest elements fast
}
另请参见:C#:按降序排序字典