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 )
Try this: