I am somewhat familiar with sorting in Scala using Ordering
's, however I would like to sort some objects which are defined in Java. They are Comparable
(not Comparable[T]
) and final
:
final class Term implements Comparable { ... }
(this is actually Lucene's Term class, and no I can't change the version of Lucene).
I first hoped there was an implicit somewhere:
terms.sorted //fail - no implicit ordering
So maybe I could make it ordered?
class OrderedTerm extends Term with Ordering[Term] //fail - class is final
After this I thought I'd 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's type parameter bounds [T <: java.lang.Comparable[_ >: T]]
So it seems even this doesn't work as Scala is strict with it's type parameters. At this point I can see two ways to go: reimplement another explicit ordering (bad) or write the sort in Java (not quite as bad).
Is there some way to do this cleanly in Scala? I assume that this situation may be common using legacy Java objects?