比较无法排序的可枚举值
Posted
技术标签:
【中文标题】比较无法排序的可枚举值【英文标题】:Compare enumerable values that can't be sorted 【发布时间】:2020-03-03 01:16:34 【问题描述】:我需要比较可枚举的值,但这些值的集合不是 ordered。
我有 Rock,Paper,Scissors
的值,例如,我希望 Rock
输给 Paper
。没有出现这种情况,看最后两行代码
object Move extends Enumeration
type Move = Value
val Rock, Paper, Scissors = Value
import Move._
object MoveOrdering extends Ordering[Move]
def compare(m1: Move, m2: Move) =
(m1, m2) match
case (Rock, Paper) => -1
case (Rock, Scissors) => +1
case (Paper, Rock) => +1
case (Paper, Scissors) => -1
case (Scissors, Paper) => +1
case (Scissors, Rock) => -1
case _ => 0
Rock > Scissors // evaluates to false, I'd expect true
Scissors < Rock // evaluates to false, I'd expect true
我遗漏了什么使上面的代码没有按预期工作?
【问题讨论】:
什么不起作用? 【参考方案1】:我认为您希望Rock > Scissors
会考虑使用MoveOrdering
。
这就是它现在的工作方式,因为abstract class Value extends Ordered[Value]
的默认实现为:
abstract class Value extends Ordered[Value] with Serializable
/** the id and bit location of this enumeration value */
def id: Int
/** a marker so we can tell whose values belong to whom come reflective-naming time */
private[Enumeration] val outerEnum = thisenum
override def compare(that: Value): Int =
if (this.id < that.id) -1
else if (this.id == that.id) 0
else 1
所以默认情况下它使用枚举中定义的元素的index
。在您的情况下,默认情况下Rock < Paper < Scissors
。如果您想使用自定义排序:
object Move extends Enumeration
type Move = Value
val Paper, Scissors, Rock = Value
import Move._
implicit object MoveOrdering extends Ordering[Move]
def compare(m1: Move, m2: Move) =
(m1, m2) match
case (Rock, Paper) => -1
case (Rock, Scissors) => +1
case (Paper, Rock) => +1
case (Paper, Scissors) => -1
case (Scissors, Paper) => +1
case (Scissors, Rock) => -1
case _ => 0
implicit val o = implicitly[Ordering[Move]]
println(List(Rock, Scissors, Paper).sorted)
println(o.lt(Rock, Scissors))
【讨论】:
以上是关于比较无法排序的可枚举值的主要内容,如果未能解决你的问题,请参考以下文章
使用比较对 Kendo Grid JQuery 进行排序 - 排序时将空值放在最后
C++ std::map sort 如何按值排序 自定义比较函数 比较对象某个字段