How to define Ordering[Array[Byte]]?
Is it necessary开发者_JS百科 to provide the implementation from scratch? I could not find any matching implicit, not even in last-resort Implicits trait. seqDerivedOrdering
obviously doesn't work, since Array
is not a Seq
.
If you want it to be efficient, you'll have to write your own (this one handles nulls; if you can assume no nulls, just use the long else block):
val o = new math.Ordering[Array[Byte]] {
def compare(a: Array[Byte], b: Array[Byte]): Int = {
if (a eq null) {
if (b eq null) 0
else -1
}
else if (b eq null) 1
else {
val L = math.min(a.length, b.length)
var i = 0
while (i < L) {
if (a(i) < b(i)) return -1
else if (b(i) < a(i)) return 1
i += 1
}
if (L < b.length) -1
else if (L < a.length) 1
else 0
}
}
}
Otherwise, you can .toSeq
to package into a WrappedArray
and defer to a Seq compare instead of doing your own scan. (This will end up boxing and unboxing your bytes, which is why it's not efficient. Since byte boxing is generally done by lookup in a table of all bytes, it's not horribly inefficent, so you may be able to get away with it unless you're doing e.g. heavy duty binary file processing.)
If you're into brevity rather than raw performance:
scala> Ordering.by((_: Array[Byte]).toIterable)
res0: scala.math.Ordering[Array[Byte]] = scala.math.Ordering$$anon$7@8c9f531
You can implement a very simple Ordering
which calls toSeq
on comapred array and calls then the seqDerivedOrdering
. The conversion to a Seq should be nearly free in term of performances.
精彩评论