scala - Sort list of string based on length -
i have list of strings
list("cbda","xyz","jlki","badce")
i want sort strings in such way odd length strings sorted in descending order , length strings sorted in ascending order
list("abcd","zyx","ijkl","edcba")
now have implemented iterating on each elements separately, finding length , sorting them accordingly. store them in separate list. hoping know if there other efficient way in scala, or shorter way (like sort of list comprehensions have in python) ?
you can sortwith , map:
list.map(s => {if(s.length % 2 == 0) s.sortwith(_ < _) else s.sortwith(_ > _)})
Comments
Post a Comment