Movatterモバイル変換


[0]ホーム

URL:


Scala 3
3.7.4
LearnInstallPlaygroundFind A LibraryCommunityBlog
Scala 3
LearnInstallPlaygroundFind A LibraryCommunityBlog
DocsAPI
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL
Scala 3/scala/scala.math/Ordering

Ordering

scala.math.Ordering
See theOrdering companion object

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 ofAnyVal (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 usingOrdering.by andOrdering.on:

import scala.util.Sortingval pairs = Array(("a", 5, 2), ("c", 3, 1), ("b", 1, 3))// sort by 2nd elementSorting.quickSort(pairs)(Ordering.by[(String, Int, Int), Int](_._2))// sort by the 3rd element, then 1stSorting.quickSort(pairs)(Ordering[(Int, String)].on(x => (x._3, x._1)))

AnOrdering[T] is implemented by specifying thecompare method,compare(a: T, b: T): Int, which decides how to order two instancesa andb. Instances ofOrdering[T] can be used by things likescala.util.Sorting to sort collections likeArray[T].

For example:

import scala.util.Sortingcase class Person(name:String, age:Int)val people = Array(Person("bob", 30), Person("ann", 32), Person("carl", 19))// sort by ageobject AgeOrdering extends Ordering[Person] { def compare(a:Person, b:Person) = a.age.compare(b.age)}Sorting.quickSort(people)(AgeOrdering)

This trait andscala.math.Ordered both provide this same functionality, but in different ways. A typeT can be given a single way to order itself by extendingOrdered. UsingOrdering, this same type may be sorted in many other ways.Ordered andOrdering both provide implicits allowing them to be used interchangeably.

You canimport scala.math.Ordering.Implicits._ to gain access to other implicit orderings.

Attributes

See also
Companion
object
Source
Ordering.scala
Graph
Supertypes
traitEquiv[T]
traitComparator[T]
classObject
traitMatchable
classAny
Show all
Known subtypes
traitNumeric[T]
traitFractional[T]
traitIntegral[T]
objectBigDecimal
objectBigInt
objectBoolean
objectByte
traitCachedReverse[T]
objectInt
objectChar
objectLong
objectShort
objectString
objectSymbol
objectUnit
Show all
Self type

Members list

Type members

Classlikes

classOrderingOps(lhs:Ordering.this.T)
Implicitly added byIterable

This inner class defines comparison operators available forT.

This inner class defines comparison operators available forT.

It can't extendAnyVal because it is not a top-level class or a member of a statically accessible object.

Attributes

Source
Ordering.scala
Supertypes
classObject
traitMatchable
classAny
classOrderingOps(lhs:Ordering.this.T)
Implicitly added byOption

This inner class defines comparison operators available forT.

This inner class defines comparison operators available forT.

It can't extendAnyVal because it is not a top-level class or a member of a statically accessible object.

Attributes

Source
Ordering.scala
Supertypes
classObject
traitMatchable
classAny
classOrderingOps(lhs:Ordering.this.T)

This inner class defines comparison operators available forT.

This inner class defines comparison operators available forT.

It can't extendAnyVal because it is not a top-level class or a member of a statically accessible object.

Attributes

Source
Ordering.scala
Supertypes
classObject
traitMatchable
classAny

Value members

Abstract methods

defcompare(x:T,y:T):Int
Implicitly added byIterable

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)

Attributes

Source
Ordering.scala
defcompare(x:T,y:T):Int
Implicitly added byOption

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)

Attributes

Source
Ordering.scala
defcompare(x:T,y:T):Int

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)

Attributes

Source
Ordering.scala

Concrete methods

overridedefequiv(x:T,y:T):Boolean

Return true ifx ==y in the ordering.

Return true ifx ==y in the ordering.

Attributes

Definition Classes
Source
Ordering.scala
overridedefgt(x:T,y:T):Boolean

Return true ifx >y in the ordering.

Return true ifx >y in the ordering.

Attributes

Definition Classes
Source
Ordering.scala
overridedefgteq(x:T,y:T):Boolean

Return true ifx >=y in the ordering.

Return true ifx >=y in the ordering.

Attributes

Definition Classes
Source
Ordering.scala
Implicitly added byIterable

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 toother == this.reverse.

Implementations should only override this method if they are overridingreverse as well.

Attributes

Source
Ordering.scala
Implicitly added byOption

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 toother == this.reverse.

Implementations should only override this method if they are overridingreverse as well.

Attributes

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 toother == this.reverse.

Implementations should only override this method if they are overridingreverse as well.

Attributes

Source
Ordering.scala
overridedeflt(x:T,y:T):Boolean

Return true ifx <y in the ordering.

Return true ifx <y in the ordering.

Attributes

Definition Classes
Source
Ordering.scala
overridedeflteq(x:T,y:T):Boolean

Return true ifx <=y in the ordering.

Return true ifx <=y in the ordering.

Attributes

Definition Classes
Source
Ordering.scala
defmax[U <:T](x:U,y:U):U
Implicitly added byIterable

Returnx ifx >=y, otherwisey.

Returnx ifx >=y, otherwisey.

Attributes

Source
Ordering.scala
defmax[U <:T](x:U,y:U):U
Implicitly added byOption

Returnx ifx >=y, otherwisey.

Returnx ifx >=y, otherwisey.

Attributes

Source
Ordering.scala
defmax[U <:T](x:U,y:U):U

Returnx ifx >=y, otherwisey.

Returnx ifx >=y, otherwisey.

Attributes

Source
Ordering.scala
defmin[U <:T](x:U,y:U):U
Implicitly added byIterable

Returnx ifx <=y, otherwisey.

Returnx ifx <=y, otherwisey.

Attributes

Source
Ordering.scala
defmin[U <:T](x:U,y:U):U
Implicitly added byOption

Returnx ifx <=y, otherwisey.

Returnx ifx <=y, otherwisey.

Attributes

Source
Ordering.scala
defmin[U <:T](x:U,y:U):U

Returnx ifx <=y, otherwisey.

Returnx ifx <=y, otherwisey.

Attributes

Source
Ordering.scala
defon[U](f:U=>T):Ordering[U]
Implicitly added byIterable

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))

Attributes

Source
Ordering.scala
defon[U](f:U=>T):Ordering[U]
Implicitly added byOption

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))

Attributes

Source
Ordering.scala
defon[U](f:U=>T):Ordering[U]

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))

Attributes

Source
Ordering.scala
deforElse(other:Ordering[T]):Ordering[T]
Implicitly added byIterable

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 ofothers 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 ofothers compare function.

Value parameters

other

an Ordering to use if this Ordering returns zero

Attributes

Example

case class Pair(a: Int, b: Int)val pairOrdering = Ordering.by[Pair, Int](_.a)                          .orElse(Ordering.by[Pair, Int](_.b))
Source
Ordering.scala
deforElse(other:Ordering[T]):Ordering[T]
Implicitly added byOption

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 ofothers 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 ofothers compare function.

Value parameters

other

an Ordering to use if this Ordering returns zero

Attributes

Example

case class Pair(a: Int, b: Int)val pairOrdering = Ordering.by[Pair, Int](_.a)                          .orElse(Ordering.by[Pair, Int](_.b))
Source
Ordering.scala
deforElse(other:Ordering[T]):Ordering[T]

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 ofothers 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 ofothers compare function.

Value parameters

other

an Ordering to use if this Ordering returns zero

Attributes

Example

case class Pair(a: Int, b: Int)val pairOrdering = Ordering.by[Pair, Int](_.a)                          .orElse(Ordering.by[Pair, Int](_.b))
Source
Ordering.scala
deforElseBy[S](f:T=>S)(implicitord:Ordering[S]):Ordering[T]
Implicitly added byIterable

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 ofOrdering.by(f) toorElse.

Attributes

Example

case class Pair(a: Int, b: Int)val pairOrdering = Ordering.by[Pair, Int](_.a)                          .orElseBy[Int](_.b)
Source
Ordering.scala
deforElseBy[S](f:T=>S)(implicitord:Ordering[S]):Ordering[T]
Implicitly added byOption

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 ofOrdering.by(f) toorElse.

Attributes

Example

case class Pair(a: Int, b: Int)val pairOrdering = Ordering.by[Pair, Int](_.a)                          .orElseBy[Int](_.b)
Source
Ordering.scala
deforElseBy[S](f:T=>S)(implicitord:Ordering[S]):Ordering[T]

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 ofOrdering.by(f) toorElse.

Attributes

Example

case class Pair(a: Int, b: Int)val pairOrdering = Ordering.by[Pair, Int](_.a)                          .orElseBy[Int](_.b)
Source
Ordering.scala
overridedefreverse:Ordering[T]

Return the opposite ordering of this one.

Return the opposite ordering of this one.

Implementations overriding this method MUST overrideisReverseOf as well if they change the behavior at all (for example, caching does not require overriding it).

Attributes

Definition Classes
Source
Ordering.scala
deftryCompare(x:T,y:T):Some[Int]
Implicitly added byIterable

Returns whether a comparison betweenx andy is defined, and if so the result ofcompare(x, y).

Returns whether a comparison betweenx andy is defined, and if so the result ofcompare(x, y).

Attributes

Source
Ordering.scala
deftryCompare(x:T,y:T):Some[Int]
Implicitly added byOption

Returns whether a comparison betweenx andy is defined, and if so the result ofcompare(x, y).

Returns whether a comparison betweenx andy is defined, and if so the result ofcompare(x, y).

Attributes

Source
Ordering.scala
deftryCompare(x:T,y:T):Some[Int]

Returns whether a comparison betweenx andy is defined, and if so the result ofcompare(x, y).

Returns whether a comparison betweenx andy is defined, and if so the result ofcompare(x, y).

Attributes

Source
Ordering.scala

Inherited methods

Implicitly added byIterable

Attributes

Inherited from:
Comparator
Implicitly added byOption

Attributes

Inherited from:
Comparator

Attributes

Inherited from:
Comparator
defthenComparing[U <:Comparable[_ >:U <:<FromJavaObject>]](x$0:Function[_ >:T <:<FromJavaObject>,_ <:U]):Comparator[T]
Implicitly added byIterable

Attributes

Inherited from:
Comparator
defthenComparing[U <:<FromJavaObject>](x$0:Function[_ >:T <:<FromJavaObject>,_ <:U],x$1:Comparator[_ >:U <:<FromJavaObject>]):Comparator[T]
Implicitly added byIterable

Attributes

Inherited from:
Comparator
defthenComparing(x$0:Comparator[_ >:T <:<FromJavaObject>]):Comparator[T]
Implicitly added byIterable

Attributes

Inherited from:
Comparator
defthenComparing[U <:Comparable[_ >:U <:<FromJavaObject>]](x$0:Function[_ >:T <:<FromJavaObject>,_ <:U]):Comparator[T]
Implicitly added byOption

Attributes

Inherited from:
Comparator
defthenComparing[U <:<FromJavaObject>](x$0:Function[_ >:T <:<FromJavaObject>,_ <:U],x$1:Comparator[_ >:U <:<FromJavaObject>]):Comparator[T]
Implicitly added byOption

Attributes

Inherited from:
Comparator
defthenComparing(x$0:Comparator[_ >:T <:<FromJavaObject>]):Comparator[T]
Implicitly added byOption

Attributes

Inherited from:
Comparator
defthenComparing[U <:Comparable[_ >:U <:<FromJavaObject>]](x$0:Function[_ >:T <:<FromJavaObject>,_ <:U]):Comparator[T]

Attributes

Inherited from:
Comparator
defthenComparing[U <:<FromJavaObject>](x$0:Function[_ >:T <:<FromJavaObject>,_ <:U],x$1:Comparator[_ >:U <:<FromJavaObject>]):Comparator[T]

Attributes

Inherited from:
Comparator
defthenComparing(x$0:Comparator[_ >:T <:<FromJavaObject>]):Comparator[T]

Attributes

Inherited from:
Comparator
defthenComparingDouble(x$0:ToDoubleFunction[_ >:T <:<FromJavaObject>]):Comparator[T]
Implicitly added byIterable

Attributes

Inherited from:
Comparator
defthenComparingDouble(x$0:ToDoubleFunction[_ >:T <:<FromJavaObject>]):Comparator[T]
Implicitly added byOption

Attributes

Inherited from:
Comparator
defthenComparingDouble(x$0:ToDoubleFunction[_ >:T <:<FromJavaObject>]):Comparator[T]

Attributes

Inherited from:
Comparator
defthenComparingInt(x$0:ToIntFunction[_ >:T <:<FromJavaObject>]):Comparator[T]
Implicitly added byIterable

Attributes

Inherited from:
Comparator
defthenComparingInt(x$0:ToIntFunction[_ >:T <:<FromJavaObject>]):Comparator[T]
Implicitly added byOption

Attributes

Inherited from:
Comparator
defthenComparingInt(x$0:ToIntFunction[_ >:T <:<FromJavaObject>]):Comparator[T]

Attributes

Inherited from:
Comparator
defthenComparingLong(x$0:ToLongFunction[_ >:T <:<FromJavaObject>]):Comparator[T]
Implicitly added byIterable

Attributes

Inherited from:
Comparator
defthenComparingLong(x$0:ToLongFunction[_ >:T <:<FromJavaObject>]):Comparator[T]
Implicitly added byOption

Attributes

Inherited from:
Comparator
defthenComparingLong(x$0:ToLongFunction[_ >:T <:<FromJavaObject>]):Comparator[T]

Attributes

Inherited from:
Comparator

Implicits

Implicits

implicitdefmkOrderingOps(lhs:T):OrderingOps
Implicitly added byIterable

This implicit method augmentsT with the comparison operators defined inscala.math.Ordering.Ops.

This implicit method augmentsT with the comparison operators defined inscala.math.Ordering.Ops.

Attributes

Source
Ordering.scala
implicitdefmkOrderingOps(lhs:T):OrderingOps
Implicitly added byOption

This implicit method augmentsT with the comparison operators defined inscala.math.Ordering.Ops.

This implicit method augmentsT with the comparison operators defined inscala.math.Ordering.Ops.

Attributes

Source
Ordering.scala
implicitdefmkOrderingOps(lhs:T):OrderingOps

This implicit method augmentsT with the comparison operators defined inscala.math.Ordering.Ops.

This implicit method augmentsT with the comparison operators defined inscala.math.Ordering.Ops.

Attributes

Source
Ordering.scala
In this article
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL

[8]ページ先頭

©2009-2025 Movatter.jp