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 8095963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:18:35+00:00 2026-06-05T21:18:35+00:00

Suppose I have a Map: val m = Map(foo -> 10, bar -> 5)

  • 0

Suppose I have a Map:

val m = Map("foo" -> 10, "bar" -> 5)

I want to convert this into a String in the following format (keys and values are separated by “:”, different elements are separated by “,”):

"bar:5,foo:10"

Note that the keys are now ordered.

How do I write a function to perform this conversion generically? I tried

def f[A, B](m: Map[A, B]): String = {
  m.toList.sortBy(_._1).map { x => x._1 + ":" + x._2 }.mkString(",")
}

But this doesn’t work, because somehow I need to specify that A is orderable. I’m not sure how to do this — I tried adding an implicit Orderer parameter to my function, but it didn’t work.

  • 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-05T21:18:36+00:00Added an answer on June 5, 2026 at 9:18 pm

    Use the Ordering typeclass:

    def f[A: Ordering, B](m: Map[A, B]): String = {
      m.toList.sortBy(_._1).map { x => x._1 + ":" + x._2 }.mkString(",")
    }
    

    This adds another parameter list to f with an implicit Ordering parameter. The method signature is actually translated to this behind the scene:

    def f[A, B](m: Map[A, B])(implicit evidence: Ordering[A]): String
    

    The evidence parameter is then picked up by sortBy and used for comparing elements.

    EDIT:

    Note that you cannot use Ordered in the same way as Ordering. The Ordered trait is meant to be mixed in with the very object type that will be sorted (like in Java). In other words, A would have to extend Ordered[A] and then you would write it as A <: Ordered[A].

    However, this inheritance approach with Ordered is less powerful than the typeclass approach with Ordering, because it is less flexible. If somebody else defined A and didn’t decide to make it extend Ordered[A], then you wouldn’t be able to use it with sortBy (at least not without creating a wrapper class). On the other hand, you can always declare a new implicit Ordering[A] in scope without changing what A inherits, and implement this Ordering in any way you like. This allows you both to define how the objects of type A are ordered when the implementer of A has forgotten to do so, and to redefine how they are ordered when you want a non-default ordering.

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

Sidebar

Related Questions

Suppose I have: stl::map<std::string, Foo> myMap; is the following function thread safe? myMap[xyz] ?
Suppose I have a STL map where the values are pointers, and I want
suppose i have the following: boost::unordered_map< string , someValueType > map; someValueType& value =
Suppose I have val foo : Seq[Double] = ... val bar : Seq[Double] =
Suppose I have the following HTML: <form id=myform> <input type='checkbox' name='foo[]'/> Check 1<br/> <input
Suppose I have a struct containing a std::string, like this: struct userdata{ int uid;
Suppose I have the following: std::map<KEY,VALUE> m1; std::map<KEY,VALUE> m2; What is the most direct
Is filter/map equivalent to list comprehension? Suppose I have the following function def fib_gen():
Suppose I have a Map of properties, and I'd like a String representation. toString
What a title, suppose I have a map like this: std::map<int, int> m; and

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.