Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7751525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:34:14+00:00 2026-06-01T11:34:14+00:00

I have an unordered map: class O(val a: Int) Map[String, List[O]] which I’d like

  • 0

I have an unordered map:

class O(val a: Int)
Map[String, List[O]]

which I’d like to turn into:

SortedMap[String, SortedMap[Int, O]]

with the child SortedMap keyed on the O field.

I’m sure there must be a more idiomatic code than the below…

class O(val a: Int)

val a: Map[String, List[O]] = Map[String, List[O]]( ("b" -> List(new O(3), new O(2))), "a" -> List(new O(1), new O(2)))

val key1s = a map (_._1)

val oMapsList = ListBuffer[SortedMap[Int, O]]()

for (key1 <- key1s) { 
  val oList = a(key1)
  val key2s = oList map (_.a)

  val sortedOMap = SortedMap[Int, O]() ++ (key2s zip oList).toMap
  oMapsList += sortedOMap
}

val sortedMap = SortedMap[String, SortedMap[Int, O]]() ++ (key1s zip oMapsList).toMap

Expected sortedMap contents is:

"a" -> ( (1 -> O(1)),(2 -> O(2)) )
"b" -> ( (2 -> O(2)),(2 -> O(3)) )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:34:15+00:00Added an answer on June 1, 2026 at 11:34 am

    Firstly, the setup:

    scala> case class O(i: Int)
    defined class O
    
    scala> Map("a" -> List(O(1), O(2)), "b" -> List(O(2), O(3)))
    res0: scala.collection.immutable.Map[java.lang.String,List[O]] = Map(a -> List(O(1), O(2)), b -> List(O(2), O(3)))
    

    Now, import SortedMap:

    scala> import collection.immutable._
    import collection.immutable._
    

    Now for the answers!


    Using breakOut (1 line of code)

    Use breakOut – but it involves some unwelcome repetition of types:

    scala> res0.map({ case (s, l) => s -> (l.map(o => o.i -> o)(collection.breakOut): SortedMap[Int, O]) })(collection.breakOut): SortedMap[String, SortedMap[Int, O]]
    res4: scala.collection.immutable.SortedMap[String,scala.collection.immutable.SortedMap[Int,O]] = Map(a -> Map(1 -> O(1), 2 -> O(2)), b -> Map(2 -> O(2), 3  -> O(3)))
    

    Using a separate method (2 lines of code)

    Or a second approach would be to involve a sort method:

    scala> def sort[K: Ordering, V](m: Traversable[(K, V)]) = SortedMap(m.toSeq: _ *)
    sort: [K, V](m: scala.collection.immutable.Traversable[(K, V)])(implicit evidence$1: Ordering[K])scala.collection.immutable.SortedMap[K,V]
    

    And so:

    scala> sort(res0.mapValues(l => sort(l.map(o => o.i -> o)) ))
    res13: scala.collection.immutable.SortedMap[java.lang.String,scala.collection.immutable.SortedMap[Int,O]] = Map(a -> Map(1 -> O(1), 2 -> O(2)), b -> Map(2 -> O(2), 3 -> O(3)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two unordered lists, a bit like: <ul class=list1> <li>Item 1</li> <li>Item 2</li>
i have a unordered list of links, which are dynamically created by Ajax, and
So I have a 3 unordered lists like so: <ul class=menu> <li class=heading>Title (Click
suppose i have the following: boost::unordered_map< string , someValueType > map; someValueType& value =
I Have a object which the following field : boost::unordered_map<std::string, std::shared_ptr<Foo> > m_liste_; I
I have an unordered list. <style> #button1 { position:relative; z-index: 3; } #button2 {
I have several unordered lists that I want to display like this: <ul> <li><img></li>
I have been trying to make a StringTable class that holds a simple unordered_map<string,
I have a object cache class like this: #include boost/thread/mutex.hpp #include boost/unordered_map.hpp template <typename
I have a MAP which is optionally of type std::map and optionally of type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.