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

  • Home
  • SEARCH
  • 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 9158545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:16:21+00:00 2026-06-17T13:16:21+00:00

This code is from a Scala Worksheet: case class E(a: Int, b: String) val

  • 0

This code is from a Scala Worksheet:

case class E(a: Int, b: String)

val l = List(
    E(1, "One"),
    E(1, "Another One"),
    E(2, "Two"),
    E(2, "Another Two"),
    E(3, "Three")
)

l.groupBy(x => x.a)                             
// res11: scala.collection.immutable.Map[Int,List[com.dci.ScratchPatch.E]] =
//    Map(
//      2 -> List(E(2,Two), E(2,Another Two)),
//      1 -> List(E(1,One), E(1,Another One)),
//      3 -> List(E(3,Three))
//    )

You will notice that groupBy returns a map, but that the ordering of the elements are now different to the way they were before. Any idea why this happens, and what the best way is to avoid this?

  • 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-17T13:16:23+00:00Added an answer on June 17, 2026 at 1:16 pm

    Unless you specifically use a subtype of SortedMap, a map (like a set) is always in an unspecified order. Since “groupBy” doesn’t return a SortedMap but only a general immutable.Map and also doesn’t use the CanBuildFrom mechanism, I think there’s nothing that you can do here.

    You can find more on this topic in answers to similar questions, e.g. here.

    Edit:

    If you want to convert the map afterwarts to a SortedMap (ordered by its keys), you can do SortedMap(l.groupBy(_.a).toSeq:_*) (with import scala.collection.immutable.SortedMap). Don’t do ...toSeq.sortWith(...).toMap because that will not guarantee the ordering in the resulting map.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the code I used from a book... import scala.actors.Actor._ val countActor =
I got this code from our frontend guy for headings: <h2 class=headline><span>Foobar</span></h2> The span
This question is related to calling scala code from java code. When I include
This is ridiculous. I am trying to download some code from scala trac: http://lampsvn.epfl.ch/trac/scala/browser/scala-tool-support/trunk/src/vim
I'm currently learning scala. Why this code doesn't work: class GenClass[T](var d : T)
I'm running this scala code on a 32-bit quad-core Core2 system: def job(i:Int,s:Int):Long =
Scala/Android newbie question. I'm trying to rewrite this simple code from Java to Scala.
Borrowing this code from several different tutorials on how to use Google maps on
In this code (from the WCF REST starterkit - preview2): protected override SampleItem OnAddItem(SampleItem
I picked up this code from a msdn blog : #include <windows.h> #include <stdio.h>

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.