paralleldrive / cuid

Collision-resistant ids optimized for horizontal scaling and performance.
Other
3.42k stars 123 forks source link

How to compare cuids #233

Closed prashantsharmamitel closed 1 year ago

prashantsharmamitel commented 3 years ago

First off, thank you for creating this useful package.

I have a requirement where I need to compare the generated cuids. I understand from the source that the generated cuids are base 36 numbers converted to strings. Taking a couple of generated cuids as an example, would following be the correct way to compare these cuids?

parseInt('ckqs83wwv00033d61mvknp45t', 36) > parseInt('ckqs81yqg00013d61fyap1rtz', 36)

ericelliott commented 2 years ago

This operation is generally not recommended in application space. Applications should not rely on the ids to tell you anything except the identity of an entity. If you want to sort things by creation date, we recommend saving the data with a createdAt field and sorting by that, instead of cuid.

If you're implementing something outside of application space, is there a good reason you can't use the default lexicographical sort order with string >, <, === in JavaScript?