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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:25:59+00:00 2026-06-18T07:25:59+00:00

I want to do something like this: class Foo extends Ordered[Foo] { val x

  • 0

I want to do something like this:

class Foo extends Ordered[Foo] {
   val x
   val y
   val z
   .
   .
   .
   .
   def compare(that: Foo) = {
      val c0 = this.length compareTo that.length          // primary comparison
      lazy val c1 = this.x compareTo that.x               // secondary comparison
      lazy val c2 = this.y.size compareTo that.y.size     // tertiary comparison
      lazy val c3 = this.z.head compareTo that.z.head     // final tie breaker
      if (c0 != 0) c0 else if (c1 != 0) c1 else if (c2 != 0) c2 else if (c3 != 0) c3 else c4
   }    
}

I was wondering if there was any cleaner way to write this kind of thing. I am expecting some thing like Ordering.multipleBy(ordering: Ordered[A]*) signature which takes a varargs of comparables and selects first non zero.

  • 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-18T07:26:00+00:00Added an answer on June 18, 2026 at 7:26 am

    It is often better to use Ordering instead of Ordered. Ordering is a type class and is much more flexible than Ordered (if only because Ordered must be implemented by the type to compare, while with Ordering you can define this outside). To define the natural ordering (default Ordering instance) for your type, you just define an implicit ordering value in the companion object.

    So, enough with the preamble. The nice thing is that when using Ordering what you want to do is pretty simple, as there is an implicit ordering for tuples (provided that the tuple elements themselves have a orderings)`:

    object Foo {
      implicit val FooOrdering = Ordering.by{ foo: Foo => 
        (foo.length, foo.x, foo.y, foo.z) 
      }
    }
    

    In addition, there is an implicit conversion that converts any value that has an Ordering type class instance into an Ordered value (see Ordered.orderingToOrdered) so we have nothing special to do to automagically being able to pass any instance of Foo to a function that expects an Ordered[Foo])


    UPDATE: Concerning your new question:

    Slightly related – is there any way to compose orderings?

    One way to do it would be to use mostly the same technic based on Ordering.by and conversion to tuples, but explicitly passing the orderings to compose:

    val byXOrdering = Ordering.by{ foo: Foo => foo.x }
    val byYOrdering = Ordering.by{ foo: Foo => foo.y }
    val byZOrdering = Ordering.by{ foo: Foo => foo.z }
    
    // Compose byXOrdering and byYOrdering:
    val byXThenYOrdering = Ordering.by{ foo: Foo => (foo, foo) }(Ordering.Tuple2(byXOrdering, byYOrdering))
    
    // Compose byXOrdering and byYOrdering and byZOrdering:
    val byXThenYThenZOrdering = Ordering.by{ foo: Foo => (foo, foo, foo) }(Ordering.Tuple3(byXOrdering, byYOrdering, byZOrdering))
    

    But it is relatively “noisy”.
    I could not find anything better using only the standard library, and so I would actually advise to use our own helper:

    final class CompositeOrdering[T]( val ord1: Ordering[T], val ord2: Ordering[T] ) extends Ordering[T] {
      def compare( x: T, y: T ) = {
        val comp = ord1.compare( x, y )
        if ( comp != 0 ) comp else ord2.compare( x, y )
      }
    }
    object CompositeOrdering {
      def apply[T]( orderings: Ordering[T] * ) = orderings reduceLeft (_ orElse _)
    }
    implicit class OrderingOps[T]( val ord: Ordering[T] ) extends AnyVal {
      def orElse( ord2: Ordering[T] ) = new CompositeOrdering[T]( ord, ord2 )
    }
    

    Which can be used like this:

    val byXOrdering = Ordering.by{ foo: Foo => foo.x }
    val byYOrdering = Ordering.by{ foo: Foo => foo.y }
    val byZOrdering = Ordering.by{ foo: Foo => foo.z }
    
    // Compose byXOrdering and byYOrdering:
    val byXThenYOrdering = byXOrdering orElse byYOrdering
    
    // Compose byXOrdering and byYOrdering and byZOrdering:
    val byXThenYThenZOrdering = byXOrdering orElse byYOrdering orElse byZOrdering
    

    Or even simpler, like this:

    // Compose byXOrdering and byYOrdering:
    val byXThenYOrdering = CompositeOrdering(byXOrdering, byYOrdering)
    
    // Compose byXOrdering and byYOrdering and byZOrdering:
    val byXThenYThenZOrdering = CompositeOrdering(byXOrdering, byYOrdering, byZOrdering)
    

    CompositeOrdering.apply is basically what you called Ordering.multipleBy in your question.

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

Sidebar

Related Questions

I have a number of classes that look like this: class Foo(val:BasicData) extends Bar(val)
I want to do something like this: class Foo(object): def __init__(self, name): self._name =
I want to do something like this: class Dictable: def dict(self): raise NotImplementedError class
What I want is actually something like this: public class Foo<T> { // ...
I want something like this: class Foo<T>{...} class Boo<T>{ Queue<T> stuff = new Queue<T>();
I want to do something like this: sealed abstract class Base(val myparam:String) case class
I want to do something like this: class Foo { bool Property { get;
I want to do something like this: class Cls { function fun($php) { return
I want to do something like this: public class ScadenzaService { ... public List<Scadenza>
I want to do something like this: template<template<int d, class> class container, int dim

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.