Ordering is a trait whose instances each represent a strategy for sorting instances of a type.
Ordering's companion object defines many implicit objects to deal with subtypes of AnyVal (e.g. Int, Double), String, and others.
To sort instances by one or more member variables, you can take advantage of these built-in orderings using Ordering.by and Ordering.on:
import scala.util.Sorting
val pairs = Array(("a", 5, 2), ("c", 3, 1), ("b", 1, 3))
// sort by 2nd element
Sorting.quickSort(pairs)(Ordering.by[(String, Int, Int), Int](_._2))
// sort by the 3rd element, then 1st
Sorting.quickSort(pairs)(Ordering[(Int, String)].on(x => (x._3, x._1)))
An Ordering[T] is implemented by specifying the compare method, compare(a: T, b: T): Int, which decides how to order two instances a and b. Instances of Ordering[T] can be used by things like scala.util.Sorting to sort collections like Array[T].
For example:
import scala.util.Sorting
case class Person(name:String, age:Int)
val people = Array(Person("bob", 30), Person("ann", 32), Person("carl", 19))
// sort by age
object AgeOrdering extends Ordering[Person] {
def compare(a:Person, b:Person) = a.age.compare(b.age)
}
Sorting.quickSort(people)(AgeOrdering)
This trait and scala.math.Ordered both provide this same functionality, but in different ways. A type T can be given a single way to order itself by extending Ordered. Using Ordering, this same type may be sorted in many other ways. Ordered and Ordering both provide implicits allowing them to be used interchangeably.
You can import scala.math.Ordering.Implicits._ to gain access to other implicit orderings.
Type parameters
- T
-
the type of objects that this ordering can compare
Attributes
- See also
- Companion
- object
- Source
- Ordering.scala
- Graph
-
- Supertypes
-
trait PartialOrdering[T]trait Equiv[T]trait Serializabletrait Comparator[T]class Objecttrait Matchableclass AnyShow all
- Known subtypes
-
object DeadlineIsOrderedobject DurationIsOrderedobject FiniteDurationIsOrderedtrait Numeric[T]trait Fractional[T]trait BigDecimalIsFractionalobject BigDecimalIsFractionaltrait DoubleIsFractionalobject DoubleIsFractionaltrait FloatIsFractionalobject FloatIsFractionaltrait Integral[T]trait BigDecimalAsIfIntegralobject BigDecimalAsIfIntegraltrait BigIntIsIntegralobject BigIntIsIntegraltrait ByteIsIntegralobject ByteIsIntegraltrait CharIsIntegralobject CharIsIntegraltrait IntIsIntegralobject IntIsIntegraltrait LongIsIntegralobject LongIsIntegraltrait ShortIsIntegralobject ShortIsIntegraltrait BigDecimalIsConflictedtrait BigDecimalOrderingobject BigDecimaltrait BigIntOrderingobject BigInttrait BooleanOrderingobject Booleantrait ByteOrderingobject Bytetrait CachedReverse[T]object Inttrait CharOrderingobject Chartrait IeeeOrderingobject IeeeOrderingtrait TotalOrderingobject DeprecatedDoubleOrderingobject TotalOrderingtrait IeeeOrderingobject IeeeOrderingtrait TotalOrderingobject DeprecatedFloatOrderingobject TotalOrderingtrait IntOrderingtrait LongOrderingobject Longtrait OptionOrdering[T]trait ShortOrderingobject Shorttrait StringOrderingobject Stringtrait SymbolOrderingobject Symboltrait UnitOrderingobject UnitShow all
- Self type
-
Ordering[T]
Members list
Type members
Classlikes
This inner class defines comparison operators available for T.
This inner class defines comparison operators available for T.
It can't extend AnyVal because it is not a top-level class or a member of a statically accessible object.
Value parameters
- lhs
-
the left-hand side value for infix comparison operations
Attributes
- Source
- Ordering.scala
- Supertypes
This inner class defines comparison operators available for T.
This inner class defines comparison operators available for T.
It can't extend AnyVal because it is not a top-level class or a member of a statically accessible object.
Value parameters
- lhs
-
the left-hand side value for infix comparison operations
Attributes
- Source
- Ordering.scala
- Supertypes
This inner class defines comparison operators available for T.
This inner class defines comparison operators available for T.
It can't extend AnyVal because it is not a top-level class or a member of a statically accessible object.
Value parameters
- lhs
-
the left-hand side value for infix comparison operations
Attributes
- Source
- Ordering.scala
- Supertypes
Value members
Abstract methods
Returns an integer whose sign communicates how x compares to y.
Returns an integer whose sign communicates how x compares to y.
The result sign has the following meaning:
- negative if x < y
- positive if x > y
- zero otherwise (if x == y)
Value parameters
- x
-
the first value to compare
- y
-
the second value to compare
Attributes
- Returns
-
a negative integer, zero, or a positive integer as
xis less than, equal to, or greater thany - Source
- Ordering.scala
Returns an integer whose sign communicates how x compares to y.
Returns an integer whose sign communicates how x compares to y.
The result sign has the following meaning:
- negative if x < y
- positive if x > y
- zero otherwise (if x == y)
Value parameters
- x
-
the first value to compare
- y
-
the second value to compare
Attributes
- Returns
-
a negative integer, zero, or a positive integer as
xis less than, equal to, or greater thany - Source
- Ordering.scala
Returns an integer whose sign communicates how x compares to y.
Returns an integer whose sign communicates how x compares to y.
The result sign has the following meaning:
- negative if x < y
- positive if x > y
- zero otherwise (if x == y)
Value parameters
- x
-
the first value to compare
- y
-
the second value to compare
Attributes
- Returns
-
a negative integer, zero, or a positive integer as
xis less than, equal to, or greater thany - Source
- Ordering.scala
Concrete methods
Returns true if x == y in the ordering.
Returns true if x == y in the ordering.
Value parameters
- x
-
the first value to compare
- y
-
the second value to compare
Attributes
- Definition Classes
- Source
- Ordering.scala
Returns true if x > y in the ordering.
Returns true if x > y in the ordering.
Value parameters
- x
-
the first value to compare
- y
-
the second value to compare
Attributes
- Definition Classes
- Source
- Ordering.scala
Returns true if x >= y in the ordering.
Returns true if x >= y in the ordering.
Value parameters
- x
-
the first value to compare
- y
-
the second value to compare
Attributes
- Definition Classes
- Source
- Ordering.scala
Returns whether or not the other ordering is the opposite ordering of this one.
Returns whether or not the other ordering is the opposite ordering of this one.
Equivalent to other == this.reverse.
Implementations should only override this method if they are overriding reverse as well.
Value parameters
- other
-
the ordering to check
Attributes
- Returns
-
trueifotheris the reverse of this ordering,falseotherwise - Source
- Ordering.scala
Returns whether or not the other ordering is the opposite ordering of this one.
Returns whether or not the other ordering is the opposite ordering of this one.
Equivalent to other == this.reverse.
Implementations should only override this method if they are overriding reverse as well.
Value parameters
- other
-
the ordering to check
Attributes
- Returns
-
trueifotheris the reverse of this ordering,falseotherwise - Source
- Ordering.scala
Returns whether or not the other ordering is the opposite ordering of this one.
Returns whether or not the other ordering is the opposite ordering of this one.
Equivalent to other == this.reverse.
Implementations should only override this method if they are overriding reverse as well.
Value parameters
- other
-
the ordering to check
Attributes
- Returns
-
trueifotheris the reverse of this ordering,falseotherwise - Source
- Ordering.scala
Returns true if x < y in the ordering.
Returns true if x < y in the ordering.
Value parameters
- x
-
the first value to compare
- y
-
the second value to compare
Attributes
- Definition Classes
- Source
- Ordering.scala
Returns true if x <= y in the ordering.
Returns true if x <= y in the ordering.
Value parameters
- x
-
the first value to compare
- y
-
the second value to compare
Attributes
- Definition Classes
- Source
- Ordering.scala
Returns x if x >= y, otherwise y.
Returns x if x >= y, otherwise y.
Type parameters
- U
-
a subtype of
T, used to preserve the specific type in the return value
Value parameters
- x
-
the first candidate value
- y
-
the second candidate value
Attributes
- Source
- Ordering.scala
Returns x if x >= y, otherwise y.
Returns x if x >= y, otherwise y.
Type parameters
- U
-
a subtype of
T, used to preserve the specific type in the return value
Value parameters
- x
-
the first candidate value
- y
-
the second candidate value
Attributes
- Source
- Ordering.scala
Returns x if x >= y, otherwise y.
Returns x if x >= y, otherwise y.
Type parameters
- U
-
a subtype of
T, used to preserve the specific type in the return value
Value parameters
- x
-
the first candidate value
- y
-
the second candidate value
Attributes
- Source
- Ordering.scala
Returns x if x <= y, otherwise y.
Returns x if x <= y, otherwise y.
Type parameters
- U
-
a subtype of
T, used to preserve the specific type in the return value
Value parameters
- x
-
the first candidate value
- y
-
the second candidate value
Attributes
- Source
- Ordering.scala
Returns x if x <= y, otherwise y.
Returns x if x <= y, otherwise y.
Type parameters
- U
-
a subtype of
T, used to preserve the specific type in the return value
Value parameters
- x
-
the first candidate value
- y
-
the second candidate value
Attributes
- Source
- Ordering.scala
Returns x if x <= y, otherwise y.
Returns x if x <= y, otherwise y.
Type parameters
- U
-
a subtype of
T, used to preserve the specific type in the return value
Value parameters
- x
-
the first candidate value
- y
-
the second candidate value
Attributes
- Source
- Ordering.scala
Given f, a function from U into T, creates an Ordering[U] whose compare function is equivalent to:
Given f, a function from U into T, creates an Ordering[U] whose compare function is equivalent to:
def compare(x:U, y:U) = Ordering[T].compare(f(x), f(y))
Type parameters
- U
-
the type of the values to be ordered
Value parameters
- f
-
the function to extract a
Tvalue from aUvalue
Attributes
- Returns
-
an
Ordering[U]that orders values by applyingfand comparing the results using this ordering - Source
- Ordering.scala
Given f, a function from U into T, creates an Ordering[U] whose compare function is equivalent to:
Given f, a function from U into T, creates an Ordering[U] whose compare function is equivalent to:
def compare(x:U, y:U) = Ordering[T].compare(f(x), f(y))
Type parameters
- U
-
the type of the values to be ordered
Value parameters
- f
-
the function to extract a
Tvalue from aUvalue
Attributes
- Returns
-
an
Ordering[U]that orders values by applyingfand comparing the results using this ordering - Source
- Ordering.scala
Given f, a function from U into T, creates an Ordering[U] whose compare function is equivalent to:
Given f, a function from U into T, creates an Ordering[U] whose compare function is equivalent to:
def compare(x:U, y:U) = Ordering[T].compare(f(x), f(y))
Type parameters
- U
-
the type of the values to be ordered
Value parameters
- f
-
the function to extract a
Tvalue from aUvalue
Attributes
- Returns
-
an
Ordering[U]that orders values by applyingfand comparing the results using this ordering - Source
- Ordering.scala
Creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else the result of others compare function.
Creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else the result of others compare function.
Value parameters
- other
-
an Ordering to use if this Ordering returns zero
Attributes
- Returns
-
an
Ordering[T]that uses this ordering first, falling back tootherwhen values are equal - Example
-
case class Pair(a: Int, b: Int) val pairOrdering = Ordering.by[Pair, Int](_.a) .orElse(Ordering.by[Pair, Int](_.b)) - Source
- Ordering.scala
Creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else the result of others compare function.
Creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else the result of others compare function.
Value parameters
- other
-
an Ordering to use if this Ordering returns zero
Attributes
- Returns
-
an
Ordering[T]that uses this ordering first, falling back tootherwhen values are equal - Example
-
case class Pair(a: Int, b: Int) val pairOrdering = Ordering.by[Pair, Int](_.a) .orElse(Ordering.by[Pair, Int](_.b)) - Source
- Ordering.scala
Creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else the result of others compare function.
Creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else the result of others compare function.
Value parameters
- other
-
an Ordering to use if this Ordering returns zero
Attributes
- Returns
-
an
Ordering[T]that uses this ordering first, falling back tootherwhen values are equal - Example
-
case class Pair(a: Int, b: Int) val pairOrdering = Ordering.by[Pair, Int](_.a) .orElse(Ordering.by[Pair, Int](_.b)) - Source
- Ordering.scala
Given f, a function from T into S, creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else a result equivalent to:
Given f, a function from T into S, creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else a result equivalent to:
Ordering[S].compare(f(x), f(y))
This function is equivalent to passing the result of Ordering.by(f) to orElse.
Type parameters
- S
-
the type returned by the extraction function
f
Value parameters
- f
-
the function to extract a comparison key from a
Tvalue - ord
-
the implicit ordering for the extracted key type
S
Attributes
- Returns
-
an
Ordering[T]that uses this ordering first, falling back to comparing byfwhen values are equal - Example
-
case class Pair(a: Int, b: Int) val pairOrdering = Ordering.by[Pair, Int](_.a) .orElseBy[Int](_.b) - Source
- Ordering.scala
Given f, a function from T into S, creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else a result equivalent to:
Given f, a function from T into S, creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else a result equivalent to:
Ordering[S].compare(f(x), f(y))
This function is equivalent to passing the result of Ordering.by(f) to orElse.
Type parameters
- S
-
the type returned by the extraction function
f
Value parameters
- f
-
the function to extract a comparison key from a
Tvalue - ord
-
the implicit ordering for the extracted key type
S
Attributes
- Returns
-
an
Ordering[T]that uses this ordering first, falling back to comparing byfwhen values are equal - Example
-
case class Pair(a: Int, b: Int) val pairOrdering = Ordering.by[Pair, Int](_.a) .orElseBy[Int](_.b) - Source
- Ordering.scala
Given f, a function from T into S, creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else a result equivalent to:
Given f, a function from T into S, creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else a result equivalent to:
Ordering[S].compare(f(x), f(y))
This function is equivalent to passing the result of Ordering.by(f) to orElse.
Type parameters
- S
-
the type returned by the extraction function
f
Value parameters
- f
-
the function to extract a comparison key from a
Tvalue - ord
-
the implicit ordering for the extracted key type
S
Attributes
- Returns
-
an
Ordering[T]that uses this ordering first, falling back to comparing byfwhen values are equal - Example
-
case class Pair(a: Int, b: Int) val pairOrdering = Ordering.by[Pair, Int](_.a) .orElseBy[Int](_.b) - Source
- Ordering.scala
Returns the opposite ordering of this one.
Returns the opposite ordering of this one.
Implementations overriding this method MUST override isReverseOf as well if they change the behavior at all (for example, caching does not require overriding it).
Attributes
- Returns
-
an
Ordering[T]that compares elements in the reverse order of this ordering - Definition Classes
- Source
- Ordering.scala
Returns whether a comparison between x and y is defined, and if so the result of compare(x, y).
Returns whether a comparison between x and y is defined, and if so the result of compare(x, y).
Value parameters
- x
-
the first value to compare
- y
-
the second value to compare
Attributes
- Source
- Ordering.scala
Returns whether a comparison between x and y is defined, and if so the result of compare(x, y).
Returns whether a comparison between x and y is defined, and if so the result of compare(x, y).
Value parameters
- x
-
the first value to compare
- y
-
the second value to compare
Attributes
- Source
- Ordering.scala
Returns whether a comparison between x and y is defined, and if so the result of compare(x, y).
Returns whether a comparison between x and y is defined, and if so the result of compare(x, y).
Value parameters
- x
-
the first value to compare
- y
-
the second value to compare
Attributes
- Source
- Ordering.scala
Inherited methods
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Implicits
Implicits
This implicit method augments T with the comparison operators defined in scala.math.Ordering.Ops.
This implicit method augments T with the comparison operators defined in scala.math.Ordering.Ops.
Value parameters
- lhs
-
the value to enrich with ordering operators
Attributes
- Source
- Ordering.scala
This implicit method augments T with the comparison operators defined in scala.math.Ordering.Ops.
This implicit method augments T with the comparison operators defined in scala.math.Ordering.Ops.
Value parameters
- lhs
-
the value to enrich with ordering operators
Attributes
- Source
- Ordering.scala
This implicit method augments T with the comparison operators defined in scala.math.Ordering.Ops.
This implicit method augments T with the comparison operators defined in scala.math.Ordering.Ops.
Value parameters
- lhs
-
the value to enrich with ordering operators
Attributes
- Source
- Ordering.scala