If you want it to be efficient, you will have to write your own (this handles null values, if you cannot accept any zeros, 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 pack .toSeq into a WrappedArray and defer it to a Seq comparison instead of a native scan. (This will lead to the boxing and unpacking of your bytes, which is why it is not efficient. Since byte-boxing is usually done by searching all the bytes in the table, it is not terribly inefficient, so you can leave it if you only process binary files with large load.)
source share