News for hackers. Stuff that matters.
Re:The surprisingly difficult problem of user-defined order ... by ramraj07 on Saturday January 16, 2021 @03:26AM (#25800941)
I actually solved this exact problem recently (who is NOT trying to recreate Roam Research amirite?) And surprised no one else suggested the solution I came up with: just use a text column? When a new list is created the items get incrementing chars as text. If a user reorders an item, the items new sort order value is the value of the item above it concatenated to an "a". This is infinitely extensible, it's only slightly a drag on storage, and very trivial to clean up with some periodic code if storage is actually getting to e a problem. Works like a charm and Dont need to modify postgres itself for this!
Re:The surprisingly difficult problem of user-defined order ... by yorwba on Saturday January 16, 2021 @04:18AM (#25801136)
> the items new sort order value is the value of the item above it concatenated to an "a"

Say you insert three items. a: 1, b: 2, c: 3.

Then you reorder 3 after 1. The new order is a: 1, aa: 3, b:2.

Then you reorder 2 after 1. The new order is a: 1, aa: 2, aa: 3.

But since there are now two values with the same sort key, it might as well be a: 1, aa: 3, aa: 2.

Did I misunderstand your solution or is that a bug?

Re:The surprisingly difficult problem of user-defined order ... by ramraj07 on Saturday January 16, 2021 @06:00AM (#25801495)
I ran into this problem and instead of solving it the proper way I took the lazy route by adding a unique constraint on the column and when it threw an errror in the insert I just added one more extra letter!
Re:The surprisingly difficult problem of user-defined order ... by n4r9 on Saturday January 16, 2021 @09:34AM (#25802385)
It's a neat idea, but this edge case makes it seem a lot like the "arbitrary precision" version of Option 2 except slightly worse on all counts. You're taking up more storage and the inserts are a lot less elegant.

Also, what about when you want to initialise a list with more than 30 elements? Do you get to "z" then continue with "za", "zb"... ?

Re:The surprisingly difficult problem of user-defined order ... by lukeramsden on Saturday January 16, 2021 @05:46AM (#25801449)
Here is a StackOverflow Q&A about something just like this: https://stackoverflow.com/questions/38923376/return-a-new-st...

A similar system is used in Jira called LexoRank