All you have to do is define an arbitrary stable ordering. (Your โobjectโs birth timeโ is one such idea, but I donโt think it is stored).
Method1: For any two objects of the same exact type, you can define this order by comparing their individual fields. If all fields are identical, the objects are equal; if not, some field f is different, and you can define an order based on the base type. If you have two objects with different types, just use the type name to determine the order; one whose name is lexicographically less is less. You can implement a comparison for each type (maybe a lot of work), or you can most likely implement a general analysis of using reflexes to list field names and types (to enable type comparisons), although this can be quite slow.
Method2: Every time you call your comparator, cache any object that is not yet in the linear array. All objects matched in this way now have an index position in the array; o1 <o2 if index (o1) Index (o2). You may need a hash table to associate the assigned index positions with cached objects.
method3: If you are working with a specific subset of objects, and there is a canonical spanning tree, then specify each edge of the spanning tree so that the child arcs have unique numbers. Then o1 <o2, if the path to o1 from the root of the spanning tree is less than the path to o2.
source share