I just typed this which seems a little ugly:
val maxTime = times.max(DateTimeComparator.getInstance().asInstanceOf[Comparator[DateTime]] asScala)
times
is a sequence of org.joda.time.DateTime.
There must be a better way to get that Ordering object for DateTime. Is there?
In particular it'd be great to lose the asInstanceOf ...
Another possibility is to use comparatorToOrdering
:
Ordering.comparatorToOrdering(DateTimeComparator.getInstance.asInstanceOf[Comparator[DateTime]])
I suppose that's what the asScala
call does. It's not prettier, I know :-|
(The cast is unfortunately required because DateTimeComparator
implements Comparator
as a raw type.)
You can also write your own class that extends the Ordering trait, and use this as input to the maximum function:
class JodaDateTimeOrdering extends Ordering[org.joda.time.DateTime] {
val dtComparer = DateTimeComparator.getInstance()
def compare(x: DateTime, y: DateTime): Int = {
dtComparer.compare(x, y)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With