compareTo

open operator override fun compareTo(other: Uuid): Int(source)

Compares this uuid with the other uuid for lexical order.

Returns zero if this uuid is lexically equal to the specified other uuid, a negative number if it's less than the other, or a positive number if it's greater than the other.

This function compares the two 128-bit uuids bit by bit sequentially, starting from the most significant bit to the least significant. this uuid is considered less than other if, at the first position where corresponding bits of the two uuids differ, the bit in this is zero and the bit in other is one. Conversely, this is considered greater than other if, at the first differing position, the bit in this is one and the bit in other is zero. If no differing bits are found, the two uuids are considered equal.

The result of the comparison of this and other uuids is equivalent to:

this.toString().compareTo(other.toString())

Since Kotlin

2.1

Samples

import kotlin.test.*
import kotlin.uuid.*

fun main() { 
   //sampleStart 
   val uuid1 = Uuid.parse("49d6d991-c780-4eb5-8585-5169c25af912")
val uuid2 = Uuid.parse("c0bac692-7208-4448-a8fe-3e3eb128db2a")
val uuid3 = Uuid.parse("49d6d991-aa92-4da0-917e-527c69621cb7")

println("uuid1 < uuid2 is ${uuid1 < uuid2}") // true
println("uuid1 > uuid3 is ${uuid1 > uuid3}") // true

val sortedUuids = listOf(uuid1, uuid2, uuid3).sorted()

println(sortedUuids[0]) // 49d6d991-aa92-4da0-917e-527c69621cb7
println(sortedUuids[1]) // 49d6d991-c780-4eb5-8585-5169c25af912
println(sortedUuids[2]) // c0bac692-7208-4448-a8fe-3e3eb128db2a 
   //sampleEnd
}