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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:58:54+00:00 2026-06-03T19:58:54+00:00

This question is probably quite dumb, but I can’t find an example and can’t

  • 0

This question is probably quite dumb, but I can’t find an example and can’t figure it out.

I want to compare two Person classes by last, first, and middle name, in that order. Here’s the brain-dead way to do it:

def compare(that: Person): Int = {
  val last: Int = lastName.compare(that.lastName)
  if (last != 0) last
  else {
    val first: Int = firstName.compare(that.firstName)
    if (first != 0) first
    else middleName.compare(that.middleName)
}

I know there’s some much more clever way to do this (probably using Ordering) but I can’t put my finger on it.

Todd

I figured out this once I realized how to access the right things in Ordering.

def compare(that: Person): Int = {
  Ordering.Tuple3(Ordering.String, Ordering.String, Ordering.String).compare(
     (lastName, firstName, middleName),
     (that.lastName, that.firstName, that.middleName))
}

I’m pretty sure I can get away with fewer explicits, but this works and is reasonably compact.

  • 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-03T19:58:55+00:00Added an answer on June 3, 2026 at 7:58 pm

    Option 1: sortBy

    Using the sortBy method, this can be pretty simple:

    case class Person(first: String, middle: String, last: String)
    val personList = List(Person("john", "a", "smith"), Person("steve", "x", "scott"), Person("bill", "w", "smith"))
    personList.sortBy{ case Person(f,m,l) => (l,f,m) } 
    

    Option 2: Extend Ordered[Person]

    By extending Ordered[Person], the class will know how to sort itself, so we get things like sorted, min, and max for free:

    case class Person(first: String, middle: String, last: String) extends Ordered[Person] {
      def compare(that: Person): Int =
        (last compare that.last) match {
          case 0 => 
            (first compare that.first) match {
              case 0 => middle compare that.middle
              case c => c
            }
          case c => c
        }
    }
    
    val personList = List(Person("john", "a", "smith"), Person("steve", "x", "scott"), Person("bill", "w", "smith"))
    personList.sorted
    personList.min
    personList.max
    

    Option 3: Implicit Ordering

    If you use an implicit Ordering, then you get sorted, min, etc without having that particular ordering tied to your original class. This decoupling might be convenient, or it might by annoying, depending on your specific case.

    case class Person(first: String, middle: String, last: String)
    
    implicit val ord = new Ordering[Person] { 
      def compare(self: Person, that: Person): Int =
        (self.last compare that.last) match {
          case 0 => 
            (self.first compare that.first) match {
              case 0 => self.middle compare that.middle
              case c => c
            }
          case c => c
        }
    }
    
    val personList = List(Person("john", "a", "smith"), Person("steve", "x", "scott"), Person("bill", "w", "smith"))
    personList.sorted
    personList.min
    personList.max
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a very obvious question but I can't quite figure it out.
This is probably quite a newb question, but I can't seem to figure it
This is probably a really simple question but one I've never quite worked out
This is probably quite a simple question, but I can't remember how to do
I know this question is probably stoopid. But I just don't want to cause
This is probably an easy question, but I want to understand better how Apache
This is probably quite a simple question but very hard to google! I'm loading
Really basic (and probably quite idiotic) question but I'm not familiar with this procedure
This will be probable quite odd question. But i thought I will give it
I realize this question has probably been asked numerous times, but I have not

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.