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

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -