Scala:Sorting.quickSort的相反顺序? [英] Scala: reverse order of Sorting.quickSort?

查看:245
本文介绍了Scala:Sorting.quickSort的相反顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此答案说明了如何使用内置方法.sortBy反转数组排序的顺序.使用scala.util.Sorting.quickSort时如何颠倒排序顺序?

This answer explains how to reverse the order of sorting an array while using its built-in method .sortBy. How do I reverse the order of the sorting when using scala.util.Sorting.quickSort?

推荐答案

像这样:

import scala.util.Sorting.quickSort

val a = Array(1, 2, 3)
quickSort[Int](a)(Ordering[Int].reverse)
//       ^---^ the most important bit
println(a.toVector) // Vector(3, 2, 1)

quickSort是一个对IntFloatDouble具有重载的函数,不允许您指定顺序,而对于具有Ordering实例的任何类型[T]的泛型函数

quickSort is a function that has overloads for Int, Float and Double, which don't let you specify ordering, and the generic one for any type [T] that has Ordering instance.

如果您有一个IntFloatDouble的数组,则重载解析将首选特殊版本,因此除非指定类型参数,否则您将无法手动传递Ordering(因此现在是编译器)只有一种选择.

If you have an array of Int, Float or Double, overload resolution will prefer specialized versions, so you will not be able to pass Ordering manually unless you specify a type parameter (so compiler now only has one choice).

对于除这三种类型(例如Long s)以外的其他事物的数组,您可以省略type参数,因为只有一个有效的选项:

For arrays of things other than these three types (e.g. Longs), you can omit the type parameter, because there's only one valid option:

val b = Array(1L, 2L, 3L)
quickSort(b)(Ordering[Long].reverse)
println(b.toVector) // Vector(3, 2, 1)

这篇关于Scala:Sorting.quickSort的相反顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆