Since Kotlin provides the programmer, to define new types in terms of classes, there must be a way to compare the instances of these classes. As in Java, the Comparable interface provides a compareTo() function to compare two objects. Kotlin provides this through the Comparable interface. However, it also provides certain extension functions, which provides much more functionality. Kotlin also provides an additional advantage that the instance implementing Comparable interface can be compared using relational operators.
This function compares the calling object with the passed object. It returns zero, if both are equal, a negative number if the passed object is greater, otherwise it returns a positive number.
abstract operator fun compareTo(other: T): Int
Extension Functions –
This function checks whether the calling object is greater than a certain minimum object. It returns current object if it is greater otherwise returns the minimum object
fun <T : Comparable> T.coerceAtLeast(minimumValue: T): T
This function checks whether the calling object is smaller than the given maximum object. It returns the current object if its smaller, otherwise returns the maximum object.
fun <T : Comparable> T.coerceAtMost(maximumValue: T): T
This function checks whether the calling object is within a certain minimum and maximum value. It returns the object if it is in the range, otherwise returns minimum if object is less than the minimum, else returns the maximum.
fun <T : Comparable> T.coerceIn( minimumValue: T?, maximumValue: T? ): T
Example to demonstrate the Comparable interface –
Is rectangle one greater than equal to rectangle two? true Is rectangle one greater than the minimum sized rectangle? true Is rectangle two smaller than the maximum sized rectangle? false Is rectangle one within the bounds? true
This function checks whether the value lie within the range or not. If value not found in range then return false, else return true. Here numbers are compared according to IEEE-754 standard.
> T.rangeTo( that: T ): ClosedRange
Kotlin program to use the rangeTo() function –
1..1000 Is 55 within the range? true Is 100000 within the range? false
- Kotlin Data Types
- Hello World program in Kotlin
- Kotlin | Retrieve Collection Parts
- Destructuring Declarations in Kotlin
- Kotlin labeled continue
- Introduction to Kotlin
- Kotlin Type Conversion
- Kotlin Exception Handling | try, catch, throw and finally
- Kotlin if-else expression
- Kotlin Environment setup for Command Line
- Kotlin constructor
- Kotlin Environment setup with Intellij IDEA
- Kotlin Nested class and Inner class
- Kotlin Variables
- Kotlin Operators
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.