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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:49:56+00:00 2026-05-21T03:49:56+00:00

I have this segment of Scala code which defines an ordering and applies it

  • 0

I have this segment of Scala code which defines an ordering and applies it to a TreeSet. This part compiles fine.

val acctOrdering = new Ordering[Account] {
  def compare(acc1: Account, acc2: Account) {

    // code to compare based on various criteria

  }
}

private var accountSet = new TreeSet[Account]()(acctOrdering)

Elsewhere in the code, I want to get the first element in the set (and later on get subsequent ones if the first one doesn’t produce what I want, although that usually won’t be necessary), based on the order I previously specified. I thought the following would work, but it didn’t compile:

val firstAccount = accountSet.min

The error is "could not find implicit value for parameter cmp: Ordering[Account]"

However, if I specify the ordering object again when asking for the minimum, it compiles:

val firstAccount = accountSet.min(acctOrdering)

I thought it would have automatically used the ordering I gave at construction time, and incrementally sorting as I added to the set, so I wouldn’t have to specify the ordering again when calling min.

What am I doing wrong? Do I need to explicitly define an implicit function somewhere?

  • 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-05-21T03:49:57+00:00Added an answer on May 21, 2026 at 3:49 am

    What is happening is that you are assuming min depends on the ordering of the set, but it doesn’t. Specifically, min and max are generic methods available on pretty much any collection, and they take an implicit Ordering parameter.

    However, if you try firstKey and lastKey, which are SortedSet-specific methods, they’ll work without having to pass any implicit.

    Edit

    One question you might have posed is to how do you make sure your Account type can be ordered by any method expecting an Ordering. You can do that by putting an implicit definition inside Account‘s object companion, like this:

    object Account {
      implicit val ord = new Ordering[Account] {
        def compare(ac1: Account, acc2: Account): Int = {
          // code to compare based on various criteria
        }
      }
    }
    

    Once you do that, you won’t need to pass the ordering explicitly.

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

Sidebar

Related Questions

I have this code segment in which I am opening/closing a file a number
I have this segment of code , a lot of things skipped for brevity
I have HTML which looks something like this: <span id=text> <span class=segment id=first>some text</span>
I have this code in jQuery, that I want to reimplement with the prototype
Ok so i have this piece of code: def findNReplaceRegExp(file_name, regexp, replaceString, verbose=True, confirmationNeeded=True):
I would like to have this x86 ASM code ported to C or C++,
I have this code that I use to bind to a repeater: Repeater rpt;
I have this example code that doesn't compile: #include <iostream> #include <vector> using std::endl;
I have this idea for a free backup application. The largest problem I need
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM

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.