如何在Scala中定义订购? [英] How to define an Ordering in Scala?
本文介绍了如何在Scala中定义订购?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
具有val hm: HashMap[org.joda.time.DateTime, MyType]
我试图分别通过hm.keys.min
和hm.keys.max
来获取集合的第一个和最后一个DateTime
,但是编译器会说No implicit Ordering defined for org.joda.time.DateTime
.如何定义这种顺序(隐式和显式选项都很有趣)?
Having val hm: HashMap[org.joda.time.DateTime, MyType]
I am trying to get the first and the last DateTime
of the set by means of hm.keys.min
and hm.keys.max
respectively but the compiler says No implicit Ordering defined for org.joda.time.DateTime
. How to define this ordering (both implicit and explicit options are interesting)?
推荐答案
object Joda {
implicit def dateTimeOrdering: Ordering[DateTime] = Ordering.fromLessThan(_ isBefore _)
}
// elsewhere
import Joda._
dateTimes.sorted
这篇关于如何在Scala中定义订购?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文