bitwalker / bitwalker/uniq

Comparing UUID7 doesn't seem to produce the expected results

Open
#21 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Elixir
Stars
121
Forks
23
PR merge metrics
No merged PRs in 30d

Description

I am attempting to sort a list of UUIDs and when I use `Uniq.UUID.compare` to do that I am not getting the list of UUIDs not sorted properly.

Here is the code used to produce and sort the UUIDs and debugging output.

Produce the UUIDs:
```elixir
uuids_with_index =
1..10
|> Enum.to_list()
|> Enum.map(fn i ->
{i, Uniq.UUID.uuid7()}
end)
```
Output:
```elixir
uuids_with_index #=> [
{1, "018dd112-d7d4-79b7-9c3f-cd8f5a268bad"},
{2, "018dd112-d7d4-7963-ac79-96d0ba13f0e3"},
{3, "018dd112-d7d4-7348-8dd5-e7a8fed42a22"},
{4, "018dd112-d7d4-7a48-a433-fcb3294b9fb3"},
{5, "018dd112-d7d4-72fb-9650-558ff08be388"},
{6, "018dd112-d7d4-7f7a-80fd-7bf8daeab8ce"},
{7, "018dd112-d7d4-7b67-8c66-f0b665a19e4f"},
{8, "018dd112-d7d4-7a80-bad6-2d0378fc51c5"},
{9, "018dd112-d7d4-74f9-a64f-2ab48c5f1ee8"},
{10, "018dd112-d7d4-74a4-804b-686439c83abc"}
]
```
Create the list and reverse it:
```elixir
uuids = Enum.map(uuids_with_index, fn {_i, uuid} -> uuid end) |> Enum.reverse()
```

Output:
``` elixir
uuids #=> ["018dd112-d7d4-74a4-804b-686439c83abc", "018dd112-d7d4-74f9-a64f-2ab48c5f1ee8",
"018dd112-d7d4-7a80-bad6-2d0378fc51c5", "018dd112-d7d4-7b67-8c66-f0b665a19e4f",
"018dd112-d7d4-7f7a-80fd-7bf8daeab8ce", "018dd112-d7d4-72fb-9650-558ff08be388",
"018dd112-d7d4-7a48-a433-fcb3294b9fb3", "018dd112-d7d4-7348-8dd5-e7a8fed42a22",
"018dd112-d7d4-7963-ac79-96d0ba13f0e3", "018dd112-d7d4-79b7-9c3f-cd8f5a268bad"]
```

Attempt to sort them:
```elixir
sorted =
Enum.sort(uuids, fn a, b ->
case Uniq.UUID.compare(a, b) do
:eq -> true
:lt -> true
:gt -> false
end
end)
```

Sorted output:
```elixir
sorted #=> ["018dd112-d7d4-7f7a-80fd-7bf8daeab8ce", "018dd112-d7d4-7b67-8c66-f0b665a19e4f",
"018dd112-d7d4-7a80-bad6-2d0378fc51c5", "018dd112-d7d4-7a48-a433-fcb3294b9fb3",
"018dd112-d7d4-79b7-9c3f-cd8f5a268bad", "018dd112-d7d4-7963-ac79-96d0ba13f0e3",
"018dd112-d7d4-74f9-a64f-2ab48c5f1ee8", "018dd112-d7d4-74a4-804b-686439c83abc",
"018dd112-d7d4-7348-8dd5-e7a8fed42a22", "018dd112-d7d4-72fb-9650-558ff08be388"]
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the reported ordering with the UUID generation and sorting snippet, then read the implementations of Uniq.UUID.uuid7/1 and Uniq.UUID.compare/2. Confirm the intended ordering for UUID7 values and add or update focused coverage for the reported sequence; done means the comparator produces the documented expected order.

Written by the indexing model from the issue text.

Assessment

Tech stack
elixir
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.