Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala map sorting

Tags:

How do I sort a map of this kind:

"01" -> List(34,12,14,23), "11" -> List(22,11,34) 

by the beginning values?

like image 709
Adrian Modliszewski Avatar asked Jan 25 '11 12:01

Adrian Modliszewski


People also ask

Can you sort a map Scala?

Solution. You can also sort by value in ascending or descending order using sortWith : // low to high scala> ListMap(grades.

Does Scala map maintain order?

Read the part of the answer about the TreeMap using Long keys that reflect when an element was inserted. If your comparator is on the insertion order, the tree will preserve the insertion order.

How do I sort a list in Scala?

Use the sortWith() Function to Sort List in Scala. We used the sortWith() function to take a lambda expression as an argument and return the sorted result. We can use this function to sort the list in ascending and descending order.

What is Scala ListMap?

Scala map is a collection of key/value pairs. Any value can be retrieved based on its key. Keys are unique in the Map, but values need not be unique. ListMap implements immutable map and uses list to implement the same. It is used with small number of elements.


2 Answers

One way is to use scala.collection.immutable.TreeMap, which is always sorted by keys:

val t = TreeMap("01" -> List(34,12,14,23), "11" -> List(22,11,34))  //If  you have already a map... val m = Map("01" -> List(34,12,14,23), "11" -> List(22,11,34)) //... use this val t = TreeMap(m.toSeq:_*) 

You can convert it to a Seq or List and sort it, too:

//by specifying an element for sorting m.toSeq.sortBy(_._1) //sort by comparing keys m.toSeq.sortBy(_._2) //sort by comparing values  //by providing a sort function m.toSeq.sortWith(_._1 < _._1) //sort by comparing keys 

There are plenty of possibilities, each more or less convenient in a certain context.

like image 102
Landei Avatar answered Sep 19 '22 11:09

Landei


As stated, the default Map type is unsorted, but there's always SortedMap

import collection.immutable.SortedMap SortedMap("01" -> List(34,12,14,23), "11" -> List(22,11,34)) 

Although I'm guessing you can't use that, because I recognise this homework and suspect that YOUR map is the result of a groupBy operation. So you have to create an empty SortedMap and add the values:

val unsorted = Map("01" -> List(34,12,14,23), "11" -> List(22,11,34)) val sorted = SortedMap.empty[String, List[Int]] ++ unsorted //or val sorted = SortedMap(unsorted.toSeq:_*) 

Or if you're not wedded to the Map interface, you can just convert it to a sequence of tuples. Note that this approach will only work if both the keys and values have a defined ordering. Lists don't have a default ordering defined, so this won't work with your example code - I therefore made up some other numbers instead.

val unsorted = Map("01" -> 56, "11" -> 34) val sorted = unsorted.toSeq.sorted 

This might be useful if you can first convert your lists to some other type (such as a String), which is best done using mapValues

update: See Landei's answer, which shows how you can provide a custom sort function that'll make this approach work.

like image 41
Kevin Wright Avatar answered Sep 22 '22 11:09

Kevin Wright