I'm wondering how can I change way in which SortedSet determines whether two objects are equal.
I have SortedSet<Tuple<Edge, int>>(new Helpers.EdgeDistanceComparer())
and Comparer method is:
public class EdgeDistanceComparer : IComparer<Tuple<Edge,int>>
{
public int Compare(Tuple<Edge, int> x, Tuple<Edge, int> y)
{
return Comparer.Default.Compare(x.Item2, y.Item2);
}
}
I believe because of this Sorted set compares only integers (Tuple.Item2), how can I do comparison on Edge class
EDIT
To explain question more:
I want to compare items by Edge class, and to sort them by Tuple.Item2, and if two Tuple.Item2 are equal, I want to add that item to SortedSet, nevertheless.
EDIT 2
Ben Gave good answer but in the end I've decided to add one more property to my class so that value I kept in Tuple.Item2 is now being saved inside property of my Edge class. And then I implemented IComparable interface, so here is how my Edge class looks like:
public class Edge : IComparable
{
public Coordinate Coordinates { get; set; }
public string Value { get; set; }
public Edge Parent { get; set; }
public int Cost { get; set; }
public int CompareTo(object obj)
{
var thatEdge = (Edge) obj;
if (Cost > thatEdge.Cost)
{
return 1;
}
if (Cost < thatEdge.Cost)
{
return -1;
}
// cost may be same but coordinates must be different
if (Cost == thatEdge.Cost &&
(Coordinates.X != thatEdge.Coordinates.X || Coordinates.Y != thatEdge.Coordinates.Y))
{
return -1;
}
return 0;
}
}
And IComparer for SortedSet:
public class EdgeDistanceComparer : IComparer<Edge>
{
public int Compare(Edge x, Edge y)
{
return Comparer.Default.Compare(x, y);
}
}