I must admit that I didn't really follow this (my bad: I'm being slow
this evening); however, it is perfectly expected for two different
values to return the same hashcode. In fact, if the IComparable says
equal, it would be curious for the hashcode to be different. Further,
for IEqualityComparer<T> (which is admitedly a bit different to
IComparable) the hashcode provision is lifted to the comparer to help
enable this (expected) behavior.
A class is worth a thousand words. I wasn't sure if comparing
hashcodes was the right thing to do. I should probably just change a
cmp == 0 to cmp = 1, but I didn't know if that would cause some
infinite loop on the sort algorithm. Consider the following class used
for both key and value in a sorted list (as part of an A* search
algorithm):
private class NodeScore : IComparable<NodeScore>
{
public NodeScore ParentNodeScore;
public Edge InputEdge;
public Node CurrentNode;
public double ScoreToEnd;
public double ScoreFromStart;
public double G
{
get { return ScoreFromStart; }
}
public double H
{
get { return ScoreToEnd; }
}
public NodeScore() { }
public NodeScore(NodeScore parent, Node Node, Edge inputEdge,
double scoreToEnd, double scoreFromStart)
{
ParentNodeScore = parent;
CurrentNode = Node;
InputEdge = inputEdge;
ScoreToEnd = scoreToEnd;
ScoreFromStart = scoreFromStart;
}
public double Score
{
get { return ScoreFromStart + ScoreToEnd; }
}
public double F
{
get { return Score; }
}
#region IComparable<NodeScore> Members
public int CompareTo(NodeScore other)
{
int cmp = Score.CompareTo(other.Score);
if (cmp == 0) // we don't allow an equal: we'll compare hashcodes
if necessary
return GetHashCode().CompareTo(other.GetHashCode());
return cmp;
}
#endregion
}