
Untitled
By: a guest on
Apr 17th, 2012 | syntax:
Scala | size: 0.59 KB | hits: 21 | expires: Never
package testing
case class Person(name: String, age: Int) extends Ordered[Person] {
def compare(other: Person) = {
if (age < other.age)
-1
else if(age > other.age)
1
else
0
}
}
object OrderingTest extends App {
val people = List(Person("ivan", 30), Person("marko", 25), Person("ana", 23))
// unsorted
println(people)
// natural sorting
println(people.sorted)
// manual sorting function, same results as above
println(people.sortWith(_.age < _.age))
// manual sorting function, same results as above
println(people.sortBy(_.age))
}