This is the companion object for thescala.math.Ordering trait.
It contains many implicit orderings as well as well as methods to construct new orderings.
An ordering which caches the value of its reverse.
An ordering which caches the value of its reverse.
Orderings forDoubles.
Orderings forDoubles.
The behavior of the comparison operations provided by the default (implicit) ordering onDouble changed in 2.10.0 and 2.13.0. Prior to Scala 2.10.0, theOrdering instance used semantics consistent withjava.lang.Double.compare.
Scala 2.10.0 changed the implementation oflt,equiv,min, etc., to be IEEE 754 compliant, while keeping thecompare method NOT compliant, creating an internally inconsistent instance. IEEE 754 specifies that0.0 == -0.0. In addition, it requires all comparisons withDouble.NaN returnfalse thus0.0 < Double.NaN,0.0 > Double.NaN, andDouble.NaN == Double.NaN all yieldfalse, analogousNone inflatMap.
Recognizing the limitation of the IEEE 754 semantics in terms of ordering, Scala 2.13.0 created two instances:Ordering.Double.IeeeOrdering, which retains the IEEE 754 semantics from Scala 2.12.x, andOrdering.Double.TotalOrdering, which brings back thejava.lang.Double.compare semantics for all operations. The default extendsTotalOrdering.
List(0.0, 1.0, 0.0 / 0.0, -1.0 / 0.0).sorted // List(-Infinity, 0.0, 1.0, NaN)List(0.0, 1.0, 0.0 / 0.0, -1.0 / 0.0).min // -Infinityimplicitly[Ordering[Double]].lt(0.0, 0.0 / 0.0) // true{ import Ordering.Double.IeeeOrdering List(0.0, 1.0, 0.0 / 0.0, -1.0 / 0.0).sorted // List(-Infinity, 0.0, 1.0, NaN) List(0.0, 1.0, 0.0 / 0.0, -1.0 / 0.0).min // NaN implicitly[Ordering[Double]].lt(0.0, 0.0 / 0.0) // false}Orderings forFloats.
Orderings forFloats.
The default extendsOrdering.Float.TotalOrdering.
Ordering.Float.TotalOrdering uses thejava.lang.Float.compare semantics for all operations. Scala also provides theOrdering.Float.IeeeOrdering semantics. Which uses the IEEE 754 semantics for float ordering.
Historically:IeeeOrdering was used in Scala from 2.10.x through 2.12.x. This changed in 2.13.0 toTotalOrdering.
Prior to Scala 2.10.0, theOrdering instance used semantics consistent withjava.lang.Float.compare.
Scala 2.10.0 changed the implementation oflt,equiv,min, etc., to be IEEE 754 compliant, while keeping thecompare method NOT compliant, creating an internally inconsistent instance. IEEE 754 specifies that0.0F == -0.0F. In addition, it requires all comparisons withFloat.NaN returnfalse thus0.0F < Float.NaN,0.0F > Float.NaN, andFloat.NaN == Float.NaN all yieldfalse, analogousNone inflatMap.
List(0.0F, 1.0F, 0.0F / 0.0F, -1.0F / 0.0F).sorted // List(-Infinity, 0.0, 1.0, NaN)List(0.0F, 1.0F, 0.0F / 0.0F, -1.0F / 0.0F).min // -Infinityimplicitly[Ordering[Float]].lt(0.0F, 0.0F / 0.0F) // true{ import Ordering.Float.IeeeOrdering List(0.0F, 1.0F, 0.0F / 0.0F, -1.0F / 0.0F).sorted // List(-Infinity, 0.0, 1.0, NaN) List(0.0F, 1.0F, 0.0F / 0.0F, -1.0F / 0.0F).min // NaN implicitly[Ordering[Float]].lt(0.0F, 0.0F / 0.0F) // false}An object containing implicits which are not in the default scope.
An object containing implicits which are not in the default scope.
Given f, a function from T into S, creates an Ordering[T] whose compare function is equivalent to:
Given f, a function from T into S, creates an Ordering[T] whose compare function is equivalent to:
def compare(x:T, y:T) = Ordering[S].compare(f(x), f(y))This function is an analogue to Ordering.on where the Ordering[S] parameter is passed implicitly.
Construct an Ordering[T] given a functionlt.
[Since version 2.13.0]Iterables are not guaranteed to have a consistent order; if using a type with a ".+("consistent order (e.g. Seq), use its Ordering (found in the Ordering.Implicits object)")Iterables are not guaranteed to have a consistent order, so theOrdering returned by this method may not be stable or meaningful. If you are using a type with a consistent order (such asSeq), use itsOrdering (found in theImplicits object) instead.
This would conflict with all the nice implicit Orderings available, but thanks to the magic of prioritized implicits via subclassing we can makeOrdered[A] => Ordering[A] only turn up if nothing else works.
This would conflict with all the nice implicit Orderings available, but thanks to the magic of prioritized implicits via subclassing we can makeOrdered[A] => Ordering[A] only turn up if nothing else works. SinceOrdered[A] extendsComparable[A] anyway, we can throw in some Java interop too.