I am a little familiar with sorting in Scala using Ordering , but I would like to sort some objects defined in Java. They are Comparable (not Comparable[T] ) and final :
final class Term implements Comparable { ... }
(this is actually the Lucene Term class, and I cannot change the version of Lucene).
At first, I hoped there was an implicit somewhere:
terms.sorted //fail - no implicit ordering
So maybe I could order it?
class OrderedTerm extends Term with Ordering[Term] //fail - class is final
After that, I thought I would resort to the nastiness of using java.util.Collections.sort :
Collections.sort(terms) // error: inferred type arguments [org.apache.lucene.index.Term] do not conform to method sort type parameter bounds [T <: java.lang.Comparable[_ >: T]]
So, it seems that even this does not work, since Scala is strict with type parameters. At this point, I see two ways: override another explicit ordering (bad) or write sorting in Java (not really like that).
Is there a way to do this cleanly in Scala? I assume this situation could be spread using legacy Java objects?
source share