如何訂購作品,我創建了下面的類並實現訂貨在斯卡拉
//class person
class Person (s:String, i:Int, d:Date) {
val name = s
override def toString = name
val age = i
val dob = d
}
/*
logic for how to compare two Person objects. Their names will be compared first. If names are same, then their age will be compared. If their age is also same, then their date of birth will be compared. Note that we could have used any other logic to compare two person objects.
*/
implicit object myPersonOrdering extends Ordering[Person] {
override def compare(x: Person, y: Person) = if (x.name.compareTo(y.name) == 0) {
if (x.age.compareTo(y.age) == 0) {
x.dob.compareTo(y.dob)
} else (x.age.compareTo(x.age))
} else (x.name.compareTo(y.name))
}
//create a list of Persons
val a1 = new Person("Andy",11, new Date())
val a2 = new Person("ann",12,new Date())
val a3 = new Person("Beth",31, new Date())
val a4 = new Person("Danny",10, new Date())
val a5 = new Person("peter",1, new Date())
val la = List(a1,a2,a3,a4, a5)
la.sortBy(x=>x)
我想明白的地方的功能比較sortBy使用的對象。查看SeqLike.scala的源代碼,sortBy方法調用方法sorted,它調用方法java.util.Arrays.sort(arr,ord.asInstanceOf [Ordering [Object]])。 compareTo是否在Java代碼中調用?