I would like to sort a List<T>
by reference. Why? I want to compare several lists and find which elements are unique to a given list. Sorting them first in some canonical fashion and then stepping through all lists simultaneously seems a good way to do this. This essentially means that I need to make aa IComparer<T>
that returns a non-zero integer for different references (and is consistent). The order does have to mean anything, but just provide a fixed order for all T
for at least the time it takes me to get the job done.
GetHashCode
can return the same value for two different objects on a 64-bit system, so that is out as a way to do a comparison.
IntPtr
from AddrOfPinnedObject()
might work since it has the correct size, but I have to pin the object for the duration of the process.
Making a Dictionary<T, long>
where the long
is issued first-come first-served is what I'm going with now, but I want to know if there are better options. The hash lookup seems unnecessary.
What is the best way to define ReferenceComparer<T> : IComparer<T>
to give me that fixed ordering?
Here is the dictionary implementation:
public class ReferenceComparer<T> : IComparer<T>
{
private long nextAvailable = 0;
private readonly Dictionary<T, long> objectToLong = new Dictionary<T, long>(new AsObjectEqualityComparer<T>());
public int Compare(T x, T y)
{
long xLong;
if (!objectToLong.TryGetValue(x, out xLong))
{
xLong = nextAvailable;
objectToLong[x] = xLong;
nextAvailable = nextAvailable + 1;
}
long yLong;
if (!objectToLong.TryGetValue(y, out yLong))
{
yLong = nextAvailable;
objectToLong[y] = yLong;
nextAvailable = nextAvailable + 1;
}
return xLong.CompareTo(yLong);
}
}