While trying to run the following snippet from Scala for the impatient:
val b = ArrayBuffer(1,7,2,9)
val bSorted = b.sorted(_ < _)
I get the following error:
error: missing parameter type for expanded function ((x$1, x$2) => x$1.$less(x$2))
val bSorted = b.sorted(_ < _)
Can somebody explain what might be going on here. Shouldn’t the parameter type be inferred from the contents of the ArrayBuffer or do I need to specify it explicitly?
Thanks
.sortedtakes an implicit parameter of typeOrdering(similar to JavaComparator). For integers, the compiler will provide the correct instance for you:If you want to pass a comparison function, use
sortWith:However, pay attention, although
ArrayBufferis mutable, both sort methods will return a copy which is sorted, but the original won’t be touched.