scala排序——ordering vs ordered
在最近探索scala的過程中,發現一個比較重要的問題,那就是排序,排序在業務程式碼中還是很常見的,最常用的排序就是對集合呼叫sorted[B >: A](implicit ord: Ordering[B])
介面,但是用久了很想問為什麼,這篇部落格目的是解釋清楚scala中的排序問題。
兩大神器
scala中提供的排序比較介面,ordering和ordered。話不多說,先貼一點原始碼看看:
trait Ordering[T] extends Comparator[T] with PartialOrdering[T] with Serializable
Ordering中集成了java中Comparator,並且同時實現了偏排序和序列化,看到Comparator相信熟悉java的觀眾朋友們肯定會很高興,心底默唸一句原來是這樣;那真的是這樣嗎?是也不是,繼續往下看。
trait Ordered[A] extends Any with java.lang.Comparable[A]
在Ordered中繼承了java中的Comparable介面,類似於java中Comparable的用法;
排序
Sorting
在scala中比較簡單的排序就是Sorting
,這個排序的介面只是對java中的Arrays.sort的簡單封裝,該介面原始碼中比較重要就是用遞迴的方式實現了一次快排,可以檢視相關原始碼,下面是對該介面的測試。
"Sorting test " should "be success" in { import scala.util.Sorting val pairs = Array(("a", 5, 2), ("c", 3, 1), ("a", 1, 3)) // 單一欄位從原型別到目標型別 Sorting.quickSort(pairs)(Ordering.by[(String, Int, Int), Int](_._2)) pairs.foreach(println) // 多個欄位,只需要指定目標型別 Sorting.quickSort(pairs)(Ordering[(String, Int)].on(x => (x._1, x._2))) pairs.foreach(println) assert(pairs.head._2 == 1, true) case class Person(name: String, age: Int) val people = Array(Person("bob", 30), Person("ann", 32), Person("carl", 19)) // sort by age object AgeOrdering extends Ordering[Person] { def compare(a: Person, b: Person): Int = a.age compare b.age } Sorting.quickSort(people)(AgeOrdering) people.foreach(println) }
執行結果:
(a,1,3)
(c,3,1)
(a,5,2)
(a,1,3)
(a,5,2)
(c,3,1)
Person(carl,19)
Person(bob,30)
Person(ann,32)
Sorting中提供了Boolean型別的排序實現,貼出來只是其註釋很有意思
// Why would you even do this?
private def booleanSort(a: Array[Boolean]): Unit = {
...
}
集合排序
除了上面的排序方式外,在集合層面,scala提供了三種排序介面,分別是
def sorted[B >: A](implicit ord: Ordering[B]) def sortWith(lt: (A, A) => Boolean) def sortBy[B](f: A => B)(implicit ord: Ordering[B])
若呼叫sorted函式做排序,則需要指定Ordering隱式引數:
val p1 = new Person("rain", 24)
val p2 = new Person("rain", 22)
val p3 = new Person("Lily", 15)
val list = List(p1, p2, p3)
implicit object PersonOrdering extends Ordering[Person] {
override def compare(p1: Person, p2: Person): Int = {
p1.name == p2.name match {
case false => -p1.name.compareTo(p2.name)
case _ => p1.age - p2.age
}
}
}
list.sorted
// res3: List[Person] = List(name: rain, age: 22, name: rain, age: 24, name: Lily, age: 15)
若使用sortWith,則需要定義返回值為Boolean的比較函式:
list.sortWith { (p1: Person, p2: Person) =>
p1.name == p2.name match {
case false => p1.name.compareTo(p2.name) < 0
case _ => p1.age - p2.age < 0
}
}
// res4: List[Person] = List(name: rain, age: 22, name: rain, age: 24, name: Lily, age: 15)
若使用sortBy,也需要指定Ordering隱式引數:
implicit object PersonOrdering extends Ordering[Person] {
override def compare(p1: Person, p2: Person): Int = {
p1.name == p2.name match {
case false => -p1.name.compareTo(p2.name)
case _ => p1.age - p2.age
}
}
}
list.sortBy[Person](t => t)
上面介紹的都是在操作層面上對已定義好的物件進行的排序,在scala靈活的語法規則中,還有以下方式的針對物件的排序定義,具體程式碼如下:
"Ordered sort " should "" in {
case class Persons(name: String, age: Int) extends Ordered[Persons] {
import scala.math.Ordered.orderingToOrdered
override def compare(that: Persons): Int = (this.name, this.age) compare (that
.name, that.age)
}
val people = Array(Persons("a", 12),Persons("a", 10),Persons("b", 9),Persons("b", 12))
people.sorted.foreach(println)
}
result :
Persons(a,10)
Persons(a,12)
Persons(b,9)
Persons(b,12)
上面這種方式類似於java中繼承了Comparable介面的實體類,具有了比較的能力,再呼叫scala.math.Ordered.orderingToOrdered
中的compare方法進行元組級別的比較,程式碼整體看起來比較簡潔,而且功能有效,而下面這種方式更為普及。
case class Employee(id: Int, firstName: String, lastName: String)
object Employee {
// Note that because `Ordering[A]` is not contravariant, the declaration
// must be type-parametrized in the event that you want the implicit
// ordering to apply to subclasses of `Employee`.
implicit def orderingByName[A <: Employee]: Ordering[A] = Ordering.by(e => (e.lastName, e.firstName))
val orderingById: Ordering[Employee] = Ordering.by(e => e.id)
}
"Ordering sort" should "" in {
Employee.orderingByName
val people = Array(Employee(11, "b", "11"),Employee(9, "a", "11"),Employee(12,
"c", "12"),Employee(10, "b", "12"),Employee(21, "a", "12"))
people.sorted.foreach(println)
println("******************************")
implicit val ord = Employee.orderingById
people.sorted.foreach(println)
}
result :
Employee(9,a,11)
Employee(11,b,11)
Employee(21,a,12)
Employee(10,b,12)
Employee(12,c,12)
******************************
Employee(9,a,11)
Employee(10,b,12)
Employee(11,b,11)
Employee(12,c,12)
Employee(21,a,12)
這種方式在伴生物件中進行了排序規則的申明,而不是在具體排序的時候,這樣做也能讓程式碼整潔,而且可以下伴生物件中根據需要定義多種排序方式,在使用時根據具體的業務場景進行選取,適合大型專案。
擴充套件:RDD sort
RDD的sortBy函式,提供根據指定的key對RDD做全域性的排序。sortBy定義如下:
def sortBy[K](
f: (T) => K,
ascending: Boolean = true,
numPartitions: Int = this.partitions.length)
(implicit ord: Ordering[K], ctag: ClassTag[K]): RDD[T]
僅需定義key的隱式轉換即可:
scala> val rdd = sc.parallelize(Array(new Person("rain", 24),
new Person("rain", 22), new Person("Lily", 15)))
scala> implicit object PersonOrdering extends Ordering[Person] {
override def compare(p1: Person, p2: Person): Int = {
p1.name == p2.name match {
case false => -p1.name.compareTo(p2.name)
case _ => p1.age - p2.age
}
}
}
scala> rdd.sortBy[Person](t => t).collect()
// res1: Array[Person] = Array(name: rain, age: 22, name: rain, age: 24, name: Lily, age: 15)
總結
在scala中總體排序方式跟在java中感覺沒有本質的區別,但是其本質區別在於scala中隱式轉換的應用以及很多工具介面的實現,可以讓你在此基礎上來進行自定義,在程式碼位置合理的情況下,減少了程式碼。