Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 如何在Scala中对数组进行排序?_Sorting_Scala - Fatal编程技术网

Sorting 如何在Scala中对数组进行排序?

Sorting 如何在Scala中对数组进行排序?,sorting,scala,Sorting,Scala,我可以看到有一个排序对象,排序,上面有一个方法,快速排序 使用它对任意类型的对象数组进行排序的代码示例是什么?看起来我需要传入Orderabletrait的实现,但我不确定语法 另外,我更喜欢用“Scala方式”来回答这个问题。我知道我可以使用Java库。排序。quickSort声明用于获取数字或字符串数组的函数,但我假设您的意思是要对自己类的对象列表进行排序 val array = Array((for(i <- 0 to 10) yield scala.util.Random.next

我可以看到有一个排序对象,
排序
,上面有一个方法,
快速排序

使用它对任意类型的对象数组进行排序的代码示例是什么?看起来我需要传入
Orderable
trait的实现,但我不确定语法


另外,我更喜欢用“Scala方式”来回答这个问题。我知道我可以使用Java库。

排序。quickSort声明用于获取数字或字符串数组的函数,但我假设您的意思是要对自己类的对象列表进行排序

val array = Array((for(i <- 0 to 10) yield scala.util.Random.nextInt): _*)
scala.util.Sorting.quickSort(array)
我想你看到的功能是

quickSort [K](a : Array[K])(implicit view$1 : (K) => Ordered[K]) : Unit
如果我读对了,这意味着数组中的对象必须具有排序特征。因此,您的类必须扩展
Ordered
(或者必须将其混合在一起),因此必须实现该特性的
compare
方法

因此,要从书中摘取一个例子:

class MyClass(n: Int) extends Ordered[MyClass] {
   ...
  def compare(that: MyClass) =
    this.n - that.n
}

因此,给定一个数组[MyClass],那么Sorting.quickSort应该可以工作。

如果您只想对事物进行排序,但没有特别考虑排序对象,那么可以使用List的排序方法。它将比较函数作为参数,因此您可以在任何类型上使用它:

List("Steve", "Tom", "John", "Bob").sort((e1, e2) => (e1 compareTo e2) < 0)

List(1, 4, 3, 2).sort((e1, e2) => (e1 < e2))
列表(“史蒂夫”、“汤姆”、“约翰”、“鲍勃”)。排序((e1,e2)=>(e1与e2的比较)<0)
列表(1,4,3,2).排序((e1,e2)=>(e1
列表可能比数组更具伸缩性

从scala api:

定义排序(lt:(A,A)=>布尔值): 名单[A]

Sort the list according to the comparison function <(e1: a, e2: a) =>
根据比较函数对列表进行排序
布尔值,如果为真 比e2小


虽然公认的答案没有错,但快速排序方法提供了更大的灵活性。我为你写了这个例子

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
*/

这显示了如何使用从Foo到某个类扩展有序[Foo]的隐式和显式转换来获得不同的排序顺序。

对于Scala 2.8或更高版本,可以执行以下操作:

List(3,7,5,2).sortWith(_ < _)
列表(3,7,5,2).sortWith

它使用的是一种快速排序的实现。

如今,这种方法也可以工作:


列表(3,7,5,2).排序

我更喜欢用户排序工具

例如:

val arr = Array(7,5,1, 9,2)

scala.util.Sorting.quickSort(arr)

请阅读此文了解更多信息

抱歉,我不清楚:我需要对任意类型的对象数组进行排序,而不是数字。是的,就是这个。现在你指出它是有道理的。我将如何“混入”?请参见编辑。您可以像我的示例一样进行扩展,也可以使用“类MyClass使用有序的[MyClass]扩展OtherClass”,这是一个混合选项。谢谢。我想我会做后一种。不是为了学究,但是…你的类不需要扩展有序,在范围内有一个视图(也称为隐式转换)从你的类到扩展有序的东西就足够了。嗯,所以List有一个排序方法,但Array没有。多么令人不满意的不规则。我期待Java API会有这种胡说八道,但Scala不会。我是一个Scala新手,不太清楚,但这可能是一个必要的缺点,因为Scala正在维护与所有Java内容的兼容性。另一方面,Scala有着如此多的魔力,它看起来很正常,以至于想要它做得更多!列表(…)排序{{{{{{}会更惯用。还值得注意的是,字符串确实定义了<方法:“daniel”<“chris”==False只是为了提供信息,在scala 2.9.1上,排序似乎不受欢迎,使用sortWithUsing隐式排序,甚至更短:List(3,7,5,2).根据我们如何知道sortWith正在使用java.util.Arrays.sort进行排序?源代码中的deepkimo这与最初的问题有点不同。它当然可以创建一个新的排序列表(或原始问题中提出的数组),但是sortWith返回一个新的对象,而不是对原始列表或数组进行排序。为什么要链接到旧的2.9.0 API文档而不是?我假设是反向排序,惯用的方式是
列表(3,7,5,2)。排序。反向
List(3,7,5,2).sortWith(_ < _)
val arr = Array(7,5,1, 9,2)

scala.util.Sorting.quickSort(arr)