Debugging sortedness

I have a class F which is quite large so I’ve left out the details. I have defined an ordering by implementing Ordered[F] on class F and its subclasses.

I get some strange behaviour which I believe may be because I have got the compare method wrong in one of the subclasses of F. I’m trying to pin down the error and have produced the below script.

println("pre sorting: " + orderedpre)   // prints List(myob1, myob2)
println(orderedpre(0))                  // prints myob1
println(orderedpre(1))                  // prints myob2
println(orderedpre(0) <= orderedpre(1)) // prints true
println(orderedpre(0) < orderedpre(1))  // prints true
println(orderedpre.sorted)        // prints: List(myob2, myob1)

This is confusing since surely if the zeroth element is <= the first, then it shouldn’t get swapped by Seq.sorted?

I note Seq.sorted uses an Ordering rather than an Ordered. Could this be the problem?

Hard to say without actual compilable code. One possibility is that there is another implicit Ordering[F] in scope with higher precedence than the Ordering[F] derived from F's Ordered[F] implementation. If you’re using an IDE, there probably is a key binding to inspect implicits used on orderedpre.sorted to explore this hypothesis.

Best regards,

Yes, there was an inconsistency in my compare methods of the subclasses, such that an myob1 < myob2 and myob2 < myob1 with myob1 != myobj2.