我看到这里有一个排序对象Sorting
,带有一个名为quickSort
的快速排序方法。
请问如何使用它来对任意类型的对象数组进行排序?看起来我需要传入一个实现了Orderable
特性的对象,但我不确定语法应该怎么写。
此外,我更倾向于按照Scala方式完成。虽然我知道可以使用Java库来完成。
现在这个也可以:
List(3,7,5,2).sorted
List(3,7,5,2).sorted.reverse
吗? - Nick ChammasSorting.quickSort声明了用于对数字或字符串数组进行排序的函数,但我猜你想要对自己定义的类对象列表进行排序?
我认为你在寻找的函数是
quickSort [K](a : Array[K])(implicit view$1 : (K) => Ordered[K]) : Unit
如果我理解正确的话,这意味着Array中的对象必须具有Ordered特质。因此您的类必须扩展Ordered(或混入它),因此必须实现该特质的compare方法。
那么我们从书中拿一个例子来看:
class MyClass(n: Int) extends Ordered[MyClass] {
...
def compare(that: MyClass) =
this.n - that.n
}
因此,如果给定一个Array[MyClass],那么Sorting.quickSort应该可以工作。
如果您只想对事物进行排序,但不必一定使用 Sorting 对象,可以使用 List 的 sort 方法。它接受一个比较函数作为参数,因此可以将其用于任何类型:
List("Steve", "Tom", "John", "Bob").sort((e1, e2) => (e1 compareTo e2) < 0)
List(1, 4, 3, 2).sort((e1, e2) => (e1 < e2))
列表可能比数组更符合Scala语言的特点。
引自Scala API 文档:
def sort(lt: (A, A) => Boolean): List[A]
Sort the list according to the comparison function <(e1: a, e2: a) =>
布尔值,当且仅当e1小于e2时应为true。
val array = Array((for(i <- 0 to 10) yield scala.util.Random.nextInt): _*)
scala.util.Sorting.quickSort(array)
scala> case class Person(name: String)
defined class Person
scala> val array = Array(Person("John"), Person("Mike"), Person("Abe"))
array: Array[Person] = Array(Person(John), Person(Mike), Person(Abe))
scala> scala.util.Sorting.quickSort(array)
<console>:11: error: no implicit argument matching parameter type (Person) => Ordered[Person] was found.
scala.util.Sorting.quickSort(array)
^
scala> class OrderedPerson(val person: Person) extends Ordered[Person] {
| def compare(that: Person) = person.name.compare(that.name)
| }
defined class OrderedPerson
scala> implicit def personToOrdered(p: Person) = new OrderedPerson(p)
personToOrdered: (p: Person)OrderedPerson
scala> scala.util.Sorting.quickSort(array)
scala> array
res8: Array[Person] = Array(Person(Abe), Person(John), Person(Mike))
现在,如果 Person 一开始就被命令执行,这个问题就不会存在:
scala> case class Person(name: String) extends Ordered[Person] {
| def compare(that: Person) = name.compare(that.name)
| }
defined class Person
scala> val array = Array(Person("John"), Person("Mike"), Person("Abe"))
array: Array[Person] = Array(Person(John), Person(Mike), Person(Abe))
scala> scala.util.Sorting.quickSort(array)
scala> array
res10: Array[Person] = Array(Person(Abe), Person(John), Person(Mike))
虽然被接受的答案并没有错,但快速排序方法提供了比那更多的灵活性。我为您编写了这个示例。
import System.out.println
import scala.util.Sorting.quickSort
class Foo(x:Int) {
def get = x
}
//a wrapper around Foo that implements Ordered[Foo]
class OrdFoo(x:Foo) extends Ordered[Foo] {
def compare(that:Foo) = x.get-that.get
}
//another wrapper around Foo that implements Ordered[Foo] in a different way
class OrdFoo2(x:Foo) extends Ordered[Foo] {
def compare(that:Foo) = that.get-x.get
}
//an implicit conversion from Foo to OrdFoo
implicit def convert(a:Foo) = new OrdFoo(a)
//an array of Foos
val arr = Array(new Foo(2),new Foo(3),new Foo(1))
//sorting using OrdFoo
scala.util.Sorting.quickSort(arr)
arr foreach (a=>println(a.get))
/*
This will print:
1
2
3
*/
//sorting using OrdFoo2
scala.util.Sorting.quickSort(arr)(new OrdFoo2(_))
arr foreach (a=>println(a.get))
/*
This will print:
3
2
1
*/