Natural Java UUID String Ordering

Say I have two instances of UUID:

uuid1 = UUID.randomUUID();
uuid2 = UUID.randomUUID();

If these two comparisons are such that uuid1less than uuid2ie,

uuid1.compareTo(uuid2) // -1

it is always true that their string representations are compared to give the same result, i.e.

uuid1.toString().compareTo(uuid2.toString()) // -1 ????
+3
source share
1 answer

In short, no. Here's an example of a quick counter example of two UUIDs for which an assertion fails:

  • b230f7ab-9420-4a3e-a684-284c609e76a5
  • 76d1f3c9-fc72-4f1a-ab48-28a858d760c5

Using compareTo from UUID, you get -1 , and compareTo from String result - 43 .

+6
source

Source: https://habr.com/ru/post/1735063/


All Articles