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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:48:22+00:00 2026-06-13T02:48:22+00:00

class P(val a: Int, val b: Int) val listp = List[P](new P(2,1)) listp.sortWith( (p1,

  • 0
class P(val a: Int, val b: Int)
val listp = List[P](new P(2,1))
listp.sortWith( (p1, p2) => p1.b < p2.b )
listp.sortBy(p => (p.b))

listp is easy to sort

val list = List((2, 1))
list.sortWith( (a1, b1), (a2, b2) => b1 < b2)   // Too many arguements
list.sortWith( ((a1, b1), (a2, b2)) => b1 < b2) // Not a legal formal parameter / <error> is already defined as <error>
list.sortBy((a, b) => (b))                      // wrong number of parameters expected

How do I sort list? All the method calls for list result in compile errors (I know I’m ignoring the return value, I just care about syntax).

I’m just looking for the syntax for the anonymous function. I know that it is possible to subclass Ordered like Comparable in Java.

Edit – This has it done for me, thanks for all answers:

list.sortWith( _._2 < _._2)
list.sortBy(_._2)
list.sortWith( (t1, t2) => t1._2 < t2._2 )

this is it explicitly

list.sortWith( (t1: Tuple2[Int, Int], t2: Tuple2[Int, Int]) => t1._2 < t2._2 )
  • 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-13T02:48:23+00:00Added an answer on June 13, 2026 at 2:48 am

    Try this:

    scala> val list = List((2, 1),(5,-10))
    list: List[(Int, Int)] = List((2,1), (5,-10))
    
    scala> list.sortWith{ case ((a1, b1), (a2, b2)) => b1 < b2}
    res1: List[(Int, Int)] = List((5,-10), (2,1))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.util.Random class Kostka { val rand = new Random(System.currentTimeMillis()) val value: List[Int] =
I have an unordered map: class O(val a: Int) Map[String, List[O]] which I'd like
Linked List Class - class MyList{ int val; MyList next; MyList(int val){ this.val =
public class Item { public List<int> val { get; set; } public double support
public class Ex7 { private int fld; private void meth(int val) { fld =
A question about inheritance in java... class Base { private int val = 10;
class A (names: String*) { val namesBuffer: ListBuffer[String] = new ListBuffer[String] } I was
Consider the following: object Main { case class Foo(bar: Int) extends FooList { val
In the example on this page: http://www.scala-lang.org/node/125 class Point(xc: Int, yc: Int) { val
Say, I have a class: class M { public int val; And also a

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.