# 在 Scala 中對陣列進行排序

Suraj P 2023年1月30日 2022年7月12日 Scala Scala Sort Scala Array

Scala 排序方法在內部使用 TimSort，這是一種合併排序和插入排序演算法的混合體。現在，讓我們看看 Scala 中存在的三種排序方法。

## 在 Scala 中使用 `sorted` 方法對陣列進行排序

`sorted` 方法用於對 Scala 中的序列進行排序，如 List、Array、Vector 和 Seq。此方法返回一個按其自然順序排序的新集合。

``````def sorted[Y >:X] (implicit ord:Ordering[Y]): Repr
``````

``````object MyClass {

def main(args: Array[String]) {

val seq = Seq(12,3,78,90,1)
println(seq.sorted)

}
}
``````

``````List(1, 3, 12, 78, 90)
``````

``````sorted(Ordering.DataType.reverse)
``````

``````object MyClass {

def main(args: Array[String]) {

val seq = Seq(12,3,78,90,1)
println(seq.sorted(Ordering.Int.reverse))

}
}
``````

``````List(90, 78, 12, 3, 1)
``````

``````case class student(id: Int,name: String) extends Ordered[student]
{
def compare(that: student) = this.name compare that.name
}

val obj1 = student(1, "tony")
val obj2 = student(2, "bruce")
val obj3 = student(3, "stark")

val studentList = List(obj1,obj2,obj3)

println(studentList.sorted)
``````

``````List(student(2,bruce), student(3,stark), student(1,tony))
``````

## 在 Scala 中使用 `sortBy(attribute)` 方法對陣列進行排序

Scala 中的 `sortBy` 方法可以基於一個或多個類屬性進行排序。如果我們在範圍中有 `Ordering` 欄位型別，則可以使用此方法。

``````def sortBy[B](f: A => B)(implicit ord: Ordering[B]): Repr
``````

``````case class employee(id: Int, name: String, salary: Double)

val obj1 = employee(1,"tony",12000.00)
val obj2 = employee(2,"bruce",11000.00)
val obj3 = employee(3,"stark",15000.00)

val empList = List(obj1,obj2,obj3)

println(empList.sortBy(_.name))

println(empList.sortBy(_.salary))
``````

``````List(employee(2,bruce,11000.0), employee(3,stark,15000.0), employee(1,tony,12000.0))
List(employee(2,bruce,11000.0), employee(1,tony,12000.0), employee(3,stark,15000.0))
``````

``````sortBy(_.attribute)(Ordering[data_type_of_attribute].reverse)
``````

``````case class employee(id: Int, name: String, salary: Double)

val obj1 = employee(1,"tony",12000.00)
val obj2 = employee(2,"bruce",11000.00)
val obj3 = employee(3,"stark",15000.00)

val empList = List(obj1,obj2,obj3)

println(empList.sortBy(_.name)(Ordering[String].reverse))
``````

``````List(employee(1,tony,12000.0), employee(3,stark,15000.0), employee(2,bruce,11000.0))
``````

``````case class employee(id: Int, name: String, salary: Double)

val obj1 = employee(1,"tony",12000.00)
val obj2 = employee(2,"bruce",11000.00)
val obj3 = employee(3,"stark",15000.00)
val obj4 = employee(4,"tony",9000.0)
val obj5 = employee(5,"stark",19000.0)
val obj6 = employee(6,"tony",10000.0)

val empList = List(obj1,obj2,obj3,obj4,obj5,obj6)

println(empList.sortBy((empList =>(empList.name,empList.salary))))
``````

``````List(employee(2,bruce,11000.0), employee(3,stark,15000.0), employee(5,stark,19000.0), employee(4,tony,9000.0), employee(6,tony,10000.0), employee(1,tony,12000.0))
``````

``````val list = List(('b',60),('c',10),('a',40))

println(list.sortBy(_._2))
``````

``````List((c,10), (a,40), (b,60))
``````

``````val list = List(('b',60),('c',10),('a',40))

println(list.sortBy(_._1))
``````

``````List((a,40), (b,60), (c,10))
``````

## 在 Scala 中使用 `sortWith` 方法對陣列進行排序

``````def sortWith(lt: (X, X) => Boolean): Repr
``````

``````case class employee(id: Int, name: String, salary: Double)

val obj1 = employee(1,"tony",12000.00)
val obj2 = employee(2,"bruce",11000.00)
val obj3 = employee(3,"stark",15000.00)
val obj4 = employee(4,"naruto",200)

val empList = List(obj1,obj2,obj3,obj4)

println(empList.sortWith(_.salary > _.salary))
``````

``````List(employee(3,stark,15000.0), employee(1,tony,12000.0), employee(2,bruce,11000.0), employee(4,naruto,200.0))
``````

``````case class employee(id: Int, name: String, salary: Double)

def sortBySalary(emp1 :employee,emp2:employee): Boolean =
{
emp1.salary < emp2.salary
}

val obj1 = employee(1,"tony",12000.00)
val obj2 = employee(2,"bruce",11000.00)
val obj3 = employee(3,"stark",15000.00)
val obj4 = employee(4,"naruto",200)

val empList = List(obj1,obj2,obj3,obj4)

println(empList.sortWith((emp1,emp2) => sortBySalary(emp1,emp2)))
``````

``````List(employee(4,naruto,200.0), employee(2,bruce,11000.0), employee(1,tony,12000.0), employee(3,stark,15000.0))
``````

Author: Suraj P

A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.