2

So, I have a few object instances, and I want to put them in a consistent order. It doesn't matter what the order is, as long as I can repeat it on any pair of instances, and get the same order. (Assume unique instances.)

Is there any way to do this without relying upon the instances' values? (I know that RuntimeHelpers.GetHashCode can get a consistent hash for each instance without worrying about its value, but the hash isn't guaranteed to be unique.)

Basically, I'm asking for an implementation of IComparer<object>.

Travis Reed
  • 1,572
  • 2
  • 8
  • 11
  • 5
    Any reason for this? Having `object` instances is weird enough already. – Camilo Terevinto Dec 03 '18 at 22:41
  • The easy solution is to generate and store your own value for each instance, although that could be a lot of memory consumed if you have a lot of objects to keep track of. – Servy Dec 03 '18 at 22:44
  • Technically I'll be using this for a more explicit reference, but since what I'm looking for should work for objects as well, I stated the less explicit definition. – Travis Reed Dec 03 '18 at 22:47
  • 1
    If you need a solution for objects whose definition you control, you could just put a static counter into the type, and have the constructor do an interlocked increment and get the latest counter and store it in the object. Now object instances can be sorted on the order in which they were created. (Of course the counter needs to have a large enough range that it will never overflow.) – Eric Lippert Dec 03 '18 at 22:56
  • I suppose the answer, then, is "it isn't worth the effort". The only reason I asked in the first place is because the more traditional sort involves some extra latency I was hoping to avoid. – Travis Reed Dec 03 '18 at 23:04
  • So you want to sort by reference _"as long as I can `repeat` it on any pair of instances"_ but without _"without relying upon the instances `values`"_. Not only is it a contradiction but it also leads to questionable usefulness. –  Dec 03 '18 at 23:28
  • Possible duplicate of [.Net equivalent of Java's System.identityHashCode()](https://stackoverflow.com/questions/53273279/net-equivalent-of-javas-system-identityhashcode) – mjwills Dec 03 '18 at 23:34
  • Possible duplicate of [Options for uniquely identifying objects at runtime?](https://stackoverflow.com/questions/4854258/options-for-uniquely-identifying-objects-at-runtime) – Brian Mar 18 '19 at 17:07

1 Answers1

4

Is there any way to do this without relying upon the instances' values?

No.

Really?

Well, you could make a weak reference table mapping objects to guids, and then your sort could look up the object in the weak reference table, get the guid, and then sort on the guid.

That's expensive, but it's doable.

It has to be a weak reference table because otherwise you're basically destroying the garbage collector's ability to free any object that's ever been sorted by your scheme.

I know that RuntimeHelpers.GetHashCode can get a consistent hash for each instance without worrying about its value, but the hash isn't guaranteed to be unique.

That's correct. If you're using GetHashCode for something other than balancing a hash table, you're almost certainly doing something wrong. It is not a unique identifier, and it is not stable.

I'm asking for an implementation of IComparer<object>.

You're going to have to live with disappointment. A total ordering on reference objects irrespective of their values is not a service that .NET provides.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • 2
    Bah, you edited it just when I was starting to add a comment involving weak references. (I was just going to suggest a list of them rather than going via guids, but...) – Jon Skeet Dec 03 '18 at 22:50