3

I have a map like:

val v = Map("01" -> List(34,12,14,23), "11" -> List(22,11,34))

I simply can't find a way to sort the list items within map.

I tried like:

v.flatMap(v => v._2.sortBy(list => list._))

but seems that it does not sort list items.

Does anybody know where I am wrong?

UPDATE: Actually my List are List of tuples like List[(Object1, Object2, Object3, Object4)], I put int for simplicity. (but I think _.sorted will not work for my objects case). So I basically want to sort by one of this objects column. (for instance Object1.int)

2 Answers 2

5

This seems to work:

v.mapValues(_.sorted)
// Map(01 -> List(12, 14, 23, 34), 11 -> List(11, 22, 34))

If your values are tuples, try this:

v.mapValues(_.sortBy(_._1))

This approach will also work for classes, just say e.g. _.sortBy(_.age).


Or if you want one, merged list:

v.values.flatten.toSeq.sorted
// List(11, 12, 14, 22, 23, 34, 34)

(for list of tuples):

v.values.flatten.toSeq.sortBy(_._1)
Sign up to request clarification or add additional context in comments.

3 Comments

Is there any way to insert in this List of tuples another element on the fly? I mean without creating new map?
@CristianBoariu: you can use mapValues() as well to map from one tuple to another. Feel free to open another question to make it clearer.
This doesn't sort in place. Is there any way to do this instead of setting it equal to a new map? Or would this be bad practice?
2

Do you mean like this?

val v = Map("01" -> List(34,12,14,23), "11" -> List(22,11,34))
//v: scala.collection.immutable.Map[String,List[Int]] = Map(01 -> List(34, 12, 14, 23), 11 -> List(22, 11, 34))

v map { case (k, v) => (k -> v.sorted) }
//res0: scala.collection.immutable.Map[String,List[Int]] = Map(01 -> List(12, 14, 23, 34), 11 -> List(11, 22, 34))

Edit or:

v mapValues (_ sorted)
//res1: scala.collection.immutable.Map[String,List[Int]] = Map(01 -> List(12, 14, 23, 34), 11 -> List(11, 22, 34))

Another edit:

If there's an implicit Ordering in scope for all the types in your tuples, it should work exactly the same way:

val v = Map(
  "01" -> List((34, "thirty-four"), (12, "twelve"), (14, "fourteen"), (23, "twenty-three")), 
  "11" -> List((22, "twenty-two"), (11, "eleven"), (34, "thirty-four")))
//v: scala.collection.immutable.Map[String,List[(Int, String)]] =
//Map(
//  01 -> List((34,thirty-four), (12,twelve), (14,fourteen), (23,twenty-three)), 
//  11 -> List((22,twenty-two), (11,eleven), (34,thirty-four)))

v mapValues (_ sorted)
//res3: scala.collection.immutable.Map[String,List[(Int, String)]] =
//Map(
//  01 -> List((12,twelve), (14,fourteen), (23,twenty-three), (34,thirty-four)),
//  11 -> List((11,eleven), (22,twenty-two), (34,thirty-four)))

If you want to sort only by one member of the tuple (say ... in this case the second one):

v mapValues (_ sortBy (_ _2))
//res5: scala.collection.immutable.Map[String,List[(Int, String)]] =
//Map(
//  01 -> List((14,fourteen), (34,thirty-four), (12,twelve), (23,twenty-three)),
//  11 -> List((11,eleven), (34,thirty-four), (22,twenty-two)))

1 Comment

or may be v mapValues { _.sorted }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.