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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:05:58+00:00 2026-06-15T16:05:58+00:00

If I have a method that has the following signature: def max[T <% Ordered[T]](list:List[T]):

  • 0

If I have a method that has the following signature:

def max[T <% Ordered[T]](list:List[T]): T={ 
 //return max. element of List (for example)
}

and I want to give a list of pairs like this to it:

val fu:List[Pair[String, Double]] = List(Pair("a", 3.1),Pair("b", 1.7),Pair("c", 3.1),Pair("d", 5.4))

How do I define the ordering on the second element of the list, so I am able to work with it in the function?

I tried to use

implicit def wrapper(p: Pair[String, Double])=new runtime.RichDouble(p._2)

to implicitly convert the Double of the Pair to a RichDouble which extends the ordered trait, but that is not working.

  • 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-15T16:05:59+00:00Added an answer on June 15, 2026 at 4:05 pm

    given:

    scala> def max[T <% Ordered[T]](xs: List[T]) = xs.max
    max: [T](xs: List[T])(implicit evidence$1: T => Ordered[T])T
    
    scala> case class Pair(x: String, y: Double)
    defined class Pair
    

    You need to define ordering object for Pair:

    scala> implicit val order = scala.math.Ordering.by[Pair, Double](_.y)
    order: scala.math.Ordering[Pair] = scala.math.Ordering$$anon$7@3b2e9a90
    

    Then you will find your max function working for Pairs

    scala> max(List(Pair("foo", 1), Pair("bar", 2), Pair("baz", 2.2)))
    res5: Pair = Pair(baz,2.2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I want to create a method that has a signature like the following:
I have a simple mapview that has the following viewdidload method and didupdate to
I have an interface (called Subject ) that has the following method: public void
I have been trying to call a C function that has the following signature
I am upgrading the following code: return this.getMethod().execute(mp); Where the execute method has signature:
I am using a method that has the following signature: public static bool TryAuthenticate(string
In my rails project I have a presenter method that has a helper view
I have a method called ReadTill that has the same body of code but
I have a very simple WCF service running that has a single method that
Possible Duplicate: Define a method that has many (or infinite) arguments I have the

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.