Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding comparison to UUID #11023

Closed
jwoertink opened this issue Jul 26, 2021 · 8 comments
Closed

Adding comparison to UUID #11023

jwoertink opened this issue Jul 26, 2021 · 8 comments
Labels
good first issue This is an issue suited for newcomers to become aquianted with working on the codebase. help wanted This issue is generally accepted and needs someone to pick it up status:discussion topic:stdlib

Comments

@jwoertink
Copy link
Contributor

Should UUIDs be comparable so Array(UUID) could be sorted alphanumerically?

require "uuid"

uuids = [UUID.new("84d9906d-b124-47b8-9632-a54ad7b43c06"), UUID.new("01cd1369-dd6e-4b3c-8131-2836dfe6102c")]

sorted = uuids.sort
 199 | v = v1 <=> v2
              ^--
Error: undefined method '<=>' for UUID

I'm assuming this wasn't originally added because UUIDs aren't normally sorted, but if you only need them in alphanumerical order, then right now you have to map them to strings. However, if you still need them as UUID, then you would have to re-map them back to UUID after sorting.

uuids.map(&.to_s).sort.map {|id| UUID.new(id)}
@asterite
Copy link
Member

sort_by &.to_s

?

@jwoertink
Copy link
Contributor Author

I guess that works too. Would there be a way to point people to using that in this case?

uuids.sort

Error: To sort Array(UUID) use `sort_by &.to_s` instead

It may just be me, but using sort_by didn't even cross my mind 😅

@Sija
Copy link
Contributor

Sija commented Jul 26, 2021

@jwoertink This looks more like a pretty typical RTFM situation ;)

@asterite
Copy link
Member

That said, maybe uuid could be sortable. We could check what other languages do.

@Blacksmoke16
Copy link
Member

Looks like Java does it: https://github.com/openjdk/jdk16/blob/master/src/java.base/share/classes/java/util/UUID.java#L496-L521.

Related https://stackoverflow.com/questions/51031195/why-java-util-uuid-is-comparable.

@asterite asterite reopened this Jul 26, 2021
@vlazar
Copy link
Contributor

vlazar commented Jul 27, 2021

@jwoertink What's your use case to sort UUID?

Maybe sortable UUID should be a thing on its own?

There are these for example:
https://github.com/SuperPaintman/ulid
https://github.com/Sija/ksuid.cr
https://github.com/wyhaines/csuuid.cr

@jwoertink
Copy link
Contributor Author

I'm creating a composite key based on the standard UUID where they are in alphabetical order. This is so I can join 2 records in the DB where their primary keys are UUID, and I don't end up with duplicate relations where they are in different orders. If they're always in alphabetical order, then I can guarantee no duplicates.

@straight-shoota straight-shoota added help wanted This issue is generally accepted and needs someone to pick it up good first issue This is an issue suited for newcomers to become aquianted with working on the codebase. labels Sep 17, 2021
@straight-shoota
Copy link
Member

Resolved by #11352

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue This is an issue suited for newcomers to become aquianted with working on the codebase. help wanted This issue is generally accepted and needs someone to pick it up status:discussion topic:stdlib
Projects
None yet
Development

No branches or pull requests

7 participants