val a = List((2,5,1),(3,8,4), (5,4,3) ,(9,1,2))
I want output as a different list which is in sorted order based on the middle element of each tuple in the list and first & third tuple's order should not be changed. Its like swapping the second tuple only.
Expected answer is:
List((2,1,1), (3,4,4) , (5,5,3), (9,8,2))
zip
and unzip
and their variants are your friends for this kind of thing.
val x = List((2,5,1),(3,8,4), (5,4,3) ,(9,1,2))
val y = x.unzip3 match {
case (a,b,c) => (a, b.sorted,c).zipped.toList
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With