disshethard
disshethard

Reputation: 83

How can I sort an ArraySeq of List by their second element?

i have to sort all Zones in europe by length.

val strefy: Seq[String] = java.util.TimeZone.getAvailableIDs.toSeq
val y = strefy.map(el =>el.split("/").toList).filter(el => el.head == "Europe")
println(y) returns => ArraySeq(List(Europe, Amsterdam), List(Europe, Andorra)......

and now i would like to sort it by lenght. I tried with sortBy(_._2).lenght but it does not work with Lists i guess. It is possible to do it without mapping List to single element? .map(el => el(1))

Upvotes: 0

Views: 86

Answers (1)

Guru Stron
Guru Stron

Reputation: 142243

._2 is for second element in tuple, to get second element in the list you can use parenthesis:

val y = strefy.map(el =>el.split("/").toList).filter(el => el.head == "Europe")
  .sortBy(_(1).length)   

or safer option - lift:

val y = strefy.map(el =>el.split("/").toList).filter(el => el.head == "Europe")
  .sortBy(_.lift(1).map(_.length))   

Upvotes: 2

Related Questions