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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:36:10+00:00 2026-05-20T04:36:10+00:00

Say I have a list of names. case class Name(val first: String, val last:

  • 0

Say I have a list of names.

case class Name(val first: String, val last: String)

val names = Name("c", "B") :: Name("b", "a") :: Name("a", "B") :: Nil

If I now want to sort that list by last name (and if that is not enough, by first name), it is easily done.

names.sortBy(n => (n.last, n.first))
// List[Name] = List(Name(a,B), Name(c,B), Name(b,a))

But what, if I‘d like to sort this list based on some other collation for strings?

Unfortunately, the following does not work:

val o  = new Ordering[String]{ def compare(x: String, y: String) = collator.compare(x, y) }
names.sortBy(n => (n.last, n.first))(o)
// error: type mismatch;
// found   : java.lang.Object with Ordering[String]
// required: Ordering[(String, String)]
//   names.sortBy(n => (n.last, n.first))(o)

is there any way that allow me to change the ordering without having to write an explicit sortWith method with multiple if–else branches in order to deal with all cases?

  • 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-20T04:36:10+00:00Added an answer on May 20, 2026 at 4:36 am

    One solution is to extend the otherwise implicitly used Tuple2 ordering. Unfortunately, this means writing out Tuple2 in the code.

    names.sortBy(n => (n.second, n.first))(Ordering.Tuple2(o, o))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

say I have a list of names var names = new List<string>{Tom, Dick, Harry};
Let's say I have a list of domain names that I would like to
Hey, Lets say we have a query which generates list of city names as
Say I have 2 parallel collections, eg: a list of people's names in a
Say I have a list and I want it arranged so that the sum
Lets say I have a following structures in c++ struct Fruit { std::string name,
say I have the following string: "Hello there. My name is Fred. I am
I have a list of parameters and I have the method name. I want
Say we have the following case: Jen Ninja (a Developer with an awesome last
Let's say I have a model class, public class NameModel { public string Title

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.