Sort the list without changing order in scala -
val = list((2,5,1),(3,8,4), (5,4,3) ,(9,1,2))
i want output different list in sorted order based on middle element of each tuple in list , first & third tuple's order should not changed. swapping second tuple only.
expected answer is:
list((2,1,1), (3,4,4) , (5,5,3), (9,8,2))
as shown below, can sort 2nd items separately , zip them together
a.zip(a.map(_._2).sorted).map{ case((a,b,c), sortedb) => (a,sortedb,c)} // res = list((2,1,1), (3,4,4), (5,5,3), (9,8,2))
Comments
Post a Comment