Let's consider the following Array[Array[Int]
val array = Array(Array(7,3,2,1), Array(3,2,5,1), Array(2,1,4,6), Array(1,2,3,4))
I would like to have this result
val sortedArray = Array(Array(1,2,3,4), Array(1,2,3,5), Array(1,2,3,7), Array(1,2,4,6))
If we know that each inner array have the same size n we start to sort each inner array then use sortBy
val sortedArray = array.map(_.sorted).sortBy(x => (x(0), x(1), x(2), x(3)))
Unfortunately if we don't know the inner array size in advance or if it's huge we cannot proceed as seen above. Maybe it is possible to define a custom ordering in a dynamic way..
In this case i can also do
val sortedArray = array.map(_.sorted).map(a => (a.reduce(_+_), a)).sortBy(_._1).map(_._2)
But it works because elements in each array are present a uniq time for each array.
array.map(_.sorted)get you the results you want ?array.map(_sorted)to have them orderedsortedArrayresult, shouldn'tArray(1,2,3,7)come beforeArray(1,2,4,6)?