protocol Comparable
Inheritance 
Equatable
View Protocol Hierarchy →


Import  import Swift 
Instance Methods
Returns a Boolean value indicating whether the value of the first argument is less than that of the second argument.
This function is the only requirement of the Comparable
protocol. The
remainder of the relational operator functions are implemented by the
standard library for any type that conforms to Comparable
.
Parameters: lhs: A value to compare. rhs: Another value to compare.
Declaration
func <(lhs: Self, rhs: Self) > Bool
Returns a Boolean value indicating whether the value of the first argument is less than or equal to that of the second argument.
Parameters: lhs: A value to compare. rhs: Another value to compare.
Declaration
func <=(lhs: Self, rhs: Self) > Bool
Returns a Boolean value indicating whether two values are equal.
Equality is the inverse of inequality. For any values a
and b
,
a == b
implies that a != b
is false
.
Parameters: lhs: A value to compare. rhs: Another value to compare.
Declaration
func ==(lhs: Self, rhs: Self) > Bool
Declared In
Equatable
Returns a Boolean value indicating whether the value of the first argument is greater than that of the second argument.
Parameters: lhs: A value to compare. rhs: Another value to compare.
Declaration
func >(lhs: Self, rhs: Self) > Bool
Returns a Boolean value indicating whether the value of the first argument is greater than or equal to that of the second argument.
Parameters: lhs: A value to compare. rhs: Another value to compare.
Declaration
func >=(lhs: Self, rhs: Self) > Bool
Default Implementations
Returns a Boolean value indicating whether two values are not equal.
Inequality is the inverse of equality. For any values a
and b
, a != b
implies that a == b
is false
.
This is the default implementation of the notequalto operator (!=
)
for any type that conforms to Equatable
.
Parameters: lhs: A value to compare. rhs: Another value to compare.
Declaration
func !=(lhs: Self, rhs: Self) > Bool
Declared In
Equatable
Returns a partial range up to, and including, its upper bound.
Use the prefix closed range operator (prefix ...
) to create a partial
range of any type that conforms to the Comparable
protocol. This
example creates a PartialRangeThrough<Double>
instance that includes
any value less than or equal to 5.0
.
let throughFive = ...5.0
throughFive.contains(4.0) // true
throughFive.contains(5.0) // true
throughFive.contains(6.0) // false
You can use this type of partial range of a collection's indices to represent the range from the start of the collection up to, and including, the partial range's upper bound.
let numbers = [10, 20, 30, 40, 50, 60, 70]
print(numbers[...3])
// Prints "[10, 20, 30, 40]"
maximum
: The upper bound for the range.
Declaration
prefix func ...(maximum: Self) > PartialRangeThrough<Self>
Returns a partial range extending upward from a lower bound.
Use the postfix range operator (postfix ...
) to create a partial range
of any type that conforms to the Comparable
protocol. This example
creates a PartialRangeFrom<Double>
instance that includes any value
greater than or equal to 5.0
.
let atLeastFive = 5.0...
atLeastFive.contains(4.0) // false
atLeastFive.contains(5.0) // true
atLeastFive.contains(6.0) // true
You can use this type of partial range of a collection's indices to represent the range from the partial range's lower bound up to the end of the collection.
let numbers = [10, 20, 30, 40, 50, 60, 70]
print(numbers[3...])
// Prints "[40, 50, 60, 70]"
minimum
: The lower bound for the range.
Declaration
postfix func ...(minimum: Self) > PartialRangeFrom<Self>
Returns a closed range that contains both of its bounds.
Use the closed range operator (...
) to create a closed range of any type
that conforms to the Comparable
protocol. This example creates a
ClosedRange<Character>
from "a" up to, and including, "z".
let lowercase = "a"..."z"
print(lowercase.contains("z"))
// Prints "true"
Parameters: minimum: The lower bound for the range. maximum: The upper bound for the range.
Declaration
func ...(minimum: Self, maximum: Self) > ClosedRange<Self>
Returns a partial range up to, but not including, its upper bound.
Use the prefix halfopen range operator (prefix ..<
) to create a
partial range of any type that conforms to the Comparable
protocol.
This example creates a PartialRangeUpTo<Double>
instance that includes
any value less than 5.0
.
let upToFive = ..<5.0
upToFive.contains(3.14) // true
upToFive.contains(6.28) // false
upToFive.contains(5.0) // false
You can use this type of partial range of a collection's indices to represent the range from the start of the collection up to, but not including, the partial range's upper bound.
let numbers = [10, 20, 30, 40, 50, 60, 70]
print(numbers[..<3])
// Prints "[10, 20, 30]"
maximum
: The upper bound for the range.
Declaration
prefix func ..<(maximum: Self) > PartialRangeUpTo<Self>
Returns a halfopen range that contains its lower bound but not its upper bound.
Use the halfopen range operator (..<
) to create a range of any type
that conforms to the Comparable
protocol. This example creates a
Range<Double>
from zero up to, but not including, 5.0.
let lessThanFive = 0.0..<5.0
print(lessThanFive.contains(3.14)) // Prints "true"
print(lessThanFive.contains(5.0)) // Prints "false"
Parameters: minimum: The lower bound for the range. maximum: The upper bound for the range.
Declaration
func ..<(minimum: Self, maximum: Self) > Range<Self>
Returns a Boolean value indicating whether the value of the first argument is less than or equal to that of the second argument.
This is the default implementation of the lessthanorequalto
operator (<=
) for any type that conforms to Comparable
.
Parameters: lhs: A value to compare. rhs: Another value to compare.
Declaration
func <=(lhs: Self, rhs: Self) > Bool
Returns a Boolean value indicating whether the value of the first argument is greater than that of the second argument.
This is the default implementation of the greaterthan operator (>
) for
any type that conforms to Comparable
.
Parameters: lhs: A value to compare. rhs: Another value to compare.
Declaration
func >(lhs: Self, rhs: Self) > Bool
Returns a Boolean value indicating whether the value of the first argument is greater than or equal to that of the second argument.
This is the default implementation of the greaterthanorequalto operator
(>=
) for any type that conforms to Comparable
.
Parameters:
lhs: A value to compare.
rhs: Another value to compare.
Returns: true
if lhs
is greater than or equal to rhs
; otherwise,
false
.
Declaration
func >=(lhs: Self, rhs: Self) > Bool
A type that can be compared using the relational operators
<
,<=
,>=
, and>
.The
Comparable
protocol is used for types that have an inherent order, such as numbers and strings. Many types in the standard library already conform to theComparable
protocol. AddComparable
conformance to your own custom types when you want to be able to compare instances using relational operators or use standard library methods that are designed forComparable
types.The most familiar use of relational operators is to compare numbers, as in the following example:
You can use special versions of some sequence and collection operations when working with a
Comparable
type. For example, if your array's elements conform toComparable
, you can call thesort()
method without using arguments to sort the elements of your array in ascending order.Conforming to the Comparable Protocol
Types with Comparable conformance implement the lessthan operator (
<
) and the equalto operator (==
). These two operations impose a strict total order on the values of a type, in which exactly one of the following must be true for any two valuesa
andb
:a == b
a < b
b < a
In addition, the following conditions must hold:
a < a
is alwaysfalse
(Irreflexivity)a < b
implies!(b < a)
(Asymmetry)a < b
andb < c
impliesa < c
(Transitivity)To add
Comparable
conformance to your custom types, define the<
and==
operators as static methods of your types. The==
operator is a requirement of theEquatable
protocol, whichComparable
extendssee that protocol's documentation for more information about equality in Swift. Because default implementations of the remainder of the relational operators are provided by the standard library, you'll be able to use!=
,>
,<=
, and>=
with instances of your type without any further code.As an example, here's an implementation of a
Date
structure that stores the year, month, and day of a date:To add
Comparable
conformance toDate
, first declare conformance toComparable
and implement the<
operator function.This function uses the least specific nonmatching property of the date to determine the result of the comparison. For example, if the two
year
properties are equal but the twomonth
properties are not, the date with the lesser value formonth
is the lesser of the two dates.Next, implement the
==
operator function, the requirement inherited from theEquatable
protocol.Two
Date
instances are equal if each of their corresponding properties is equal.Now that
Date
conforms toComparable
, you can compare instances of the type with any of the relational operators. The following example compares the date of the first moon landing with the release of David Bowie's song "Space Oddity":Note that the
>
operator provided by the standard library is used in this example, not the<
operator implemented above.Note: A conforming type may contain a subset of values which are treated as exceptionalthat is, values that are outside the domain of meaningful arguments for the purposes of the
Comparable
protocol. For example, the special "not a number" value for floatingpoint types (FloatingPoint.nan
) compares as neither less than, greater than, nor equal to any normal floatingpoint value. Exceptional values need not take part in the strict total order.