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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:06:18+00:00 2026-05-26T15:06:18+00:00

I am somewhat familiar with sorting in Scala using Ordering ‘s, however I would

  • 0

I am somewhat familiar with sorting in Scala using Ordering‘s, however I would like to sort some objects which are defined in Java. They are Comparable (not Comparable[T]) and final:

final class Term implements Comparable { ... }

(this is actually Lucene’s Term class, and no I can’t change the version of Lucene).

I first hoped there was an implicit somewhere:

terms.sorted //fail - no implicit ordering

So maybe I could make it ordered?

class OrderedTerm extends Term with Ordering[Term] //fail - class is final

After this I thought I’d resort to the nastiness of using java.util.Collections.sort:

Collections.sort(terms) // error: inferred type arguments [org.apache.lucene.index.Term] do not conform to method sort's type parameter bounds [T <: java.lang.Comparable[_ >: T]]

So it seems even this doesn’t work as Scala is strict with it’s type parameters. At this point I can see two ways to go: reimplement another explicit ordering (bad) or write the sort in Java (not quite as bad).

Is there some way to do this cleanly in Scala? I assume that this situation may be common using legacy Java objects?

  • 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-26T15:06:19+00:00Added an answer on May 26, 2026 at 3:06 pm

    Ordering (as opposed to Ordered) is separate from the compared type. It is equivalent to java Comparator, not Comparable. So you simply define you ordering on Terms as a singleton, there is no problem with inheriting Term.

    implicit object TermOrdering extends Ordering[Term] {
      def compare(t1: Term, t2: Term: Term): Int = ....
    }
    

    Better mark it implicit because it will be convenient to have it in implicit scope. Then you just have to ensure that TermOdering is imported when you call some operation that needs it.

    P.S. You should read this great answer by Daniel Sobral.

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

Sidebar

Related Questions

I am somewhat familiar with using configuration in .NET, but I am a little
I'm somewhat familiar with Cocoa, Objective-C and iPhone Development using Xcode. I'm starting to
(Somewhat of a follow on from the post (which remains unanswered): https://stackoverflow.com/q/6197829/314661 ) Using
I'm somewhat familiar with displaying simple data via a JMX MBean using older JDKs.
I'm learning how to use class_eval in modules (I'm somewhat familiar with class_eval) and
Where I work we're still on .Net 2.0, but I'm somewhat familiar with the
I don't mind learning xaml and I'm sure I need to be familiar somewhat,
I'm somewhat familiar with WCF in that I can build Web Services in VS.Net
I am only somewhat familiar with multi-threading in that I've read about it but
I'm somewhat familiar with the various methods of performing non-blocking IO within PHP, but

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.