如何在 Scala 中将 DateTimeComparator 转换为 Ordering[DateTime]

发布于 2024-11-02 23:27:00 字数 296 浏览 1 评论 0原文

我刚刚输入了这个看起来有点难看的内容:

val maxTime = times.max(DateTimeComparator.getInstance().asInstanceOf[Comparator[DateTime]] asScala)

times 是 org.joda.time.DateTime 的序列。

必须有更好的方法来获取 DateTime 的 Ordering 对象。有没有?

特别是失去 asInstanceOf 会很棒......

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 ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小嗲 2024-11-09 23:27:00

另一种可能性是使用 comparatorToOrdering

Ordering.comparatorToOrdering(DateTimeComparator.getInstance.asInstanceOf[Comparator[DateTime]])

我想这就是 asScala 调用的作用。我知道它并不漂亮:-|

(不幸的是,需要进行强制转换,因为 DateTimeComparatorComparator 实现为原始类型。)

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.)

剪不断理还乱 2024-11-09 23:27:00

您还可以编写自己的类来扩展 Ordering 特征,并将其用作最大值函数的输入:

class JodaDateTimeOrdering extends Ordering[org.joda.time.DateTime] {
  val dtComparer = DateTimeComparator.getInstance()

  def compare(x: DateTime, y: DateTime): Int = {
    dtComparer.compare(x, y)
  }
}

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)
  }
}
无尽的现实 2024-11-09 23:27:00
times.max(Ordering.fromLessThan[DateTime](
  DateTimeComparator.getInstance.compare(_,_) < 0))

这也太丑了!

你的 asScala 来自哪里?

其他想法

我不确定是否有更好的方法。 DateComparator 实现Comparator

max 方法需要一个 Ordering[DateTime]。 Ordered 和 Ordering 在 Scala 中是不变的。所以我认为这种情况下有必要使用asScala

times.max(Ordering.fromLessThan[DateTime](
  DateTimeComparator.getInstance.compare(_,_) < 0))

which is ugly too!

Where does your asScala come from?

additional thoughts

I'm not sure there is a better way. DateComparator implements Comparator.

the max method expects an Ordering[DateTime]. Ordered and Ordering are invariant in Scala. So I think the case is necessary to use asScala.

痴意少年 2024-11-09 23:27:00

最好的可能性,但有额外的依赖: NScala-Time 包装器 for Joda DateTime 隐含了 DateTimeOrdering:

https://github.com/nscala-time/nscala-time/blob/master/src/main/scala/com/github/nscala_time/time/Implicits.scala

Best possibility, but with additional dependency: NScala-Time wrapper for Joda DateTime has DateTimeOrdering implicit:

https://github.com/nscala-time/nscala-time/blob/master/src/main/scala/com/github/nscala_time/time/Implicits.scala

你在我安 2024-11-09 23:27:00

如果您愿意使用 Saddle,那里定义了隐式日期时间排序,在这种情况下,这足以解决问题:

import org.saddle.time._

In case you are willing to use Saddle, there's an implicit Datetime ordering defined there, in which case this is enough to solve the issue:

import org.saddle.time._
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文