---
title: "How to Store a Sortable List in a Database (LexoRank)"
date: "2024-05-06"
description: "Seven ways to save drag and drop order in a SQL database: position numbers, linked lists, JSON, gaps, decimals and LexoRank buckets, with the trade offs of each."
tags: ["Tutorials", "Work", "Product Design"]
url: "https://rolandoandrade.me/blog/storing-sortable-items-in-a-relational-database/"
author: "Rolando Andrade"
---

# How to Store a Sortable List in a Database (LexoRank)

> Seven ways to save drag and drop order in a SQL database: position numbers, linked lists, JSON, gaps, decimals and LexoRank buckets, with the trade offs of each.

Hey there! How is it going? Last week, while I was working on **Bezier**, I hit a problem I had never seen before. After some thinking, I found a solution. I want to share it today, in case it helps someone with the same problem.

## A list you can reorder

Imagine a drag and drop sortable list, like the one below. You can move the items into any order you like.

![Easy, isn't?](https://storage.rolandoandrade.me/blog/drag-drop-list-gif.gif)

Here is the catch. We store the data in **Cloudflare D1**. So we must solve this with a relational database (SQLite), a database of tables and rows. We also need as few reads and writes as possible, to keep performance high and costs low.

The app is offline first, so it keeps working without internet. Still, real time sync is critical. Every change must reach the database the moment it happens.

With these limits in mind, can you think of a solution?

## Attempt 1: a position number

The first solution was simple. Add a `position` field that goes from 0 to N. To move element *j* to position *i*, you update every element in the range `[min(i, j), max(i, j))` with its new position.

![](https://storage.rolandoandrade.me/blog/id-diagram-1.webp)

Nothing unusual here. It is a classic insertion algorithm. The problem? Every single change updates many records. That gets expensive, and slow, very fast.

## Attempt 2: a linked list

What about a linked list? Each item gets `next` and `prev` cursors that point to the IDs of its neighbors. To reorder, you only update a few records to build the new sequence.

![](https://storage.rolandoandrade.me/blog/id-diagram-2.webp)

This is better and more predictable than shifting a whole range, but it still touches several rows. It is also harder to query efficiently. And when many people edit at once, the links can easily end up broken.

## Attempt 3: one JSON field

At this point, the problem seemed to be SQL itself. A document or graph database might make this easier. So why not store the items in one JSON field instead of separate rows?

![](https://storage.rolandoandrade.me/blog/id-diagram-3.webp)

This is a valid solution, but it does not work for my case. My items have complex relations and need specific queries. Those queries would be painful inside a block of JSON. I cannot give up the guarantees of relational data. Still, if nothing better existed, I might have accepted this one.

## Attempt 4: a JSON order field

Okay. If relations matter, why not keep the order apart from the items? We could add a single "order" field that holds an array of IDs in the order we show them.

![](https://storage.rolandoandrade.me/blog/id-diagram-4.webp)

This makes sense. It solves the problem and updates only one field. Perfect, right? Well, I believe there are no perfect solutions, only the best trade off for each case. This can work for simple lists. But nested items, lists inside lists, make it complicated fast. I did not feel comfortable with it, but it is worth mentioning if your needs are simpler.

## Attempt 5: numbers with gaps

Let's go back to the first solution. Its problem was updating huge ranges for one move. But what if we did not use numbers in a row (1, 2, 3)? What if we left gaps between them?

![](https://storage.rolandoandrade.me/blog/id-diagram-5.webp)

To move an item, we calculate the midpoint between its new neighbors. It works very well, until it does not. Say we move `item 2` back to the second position again and again.

![](https://storage.rolandoandrade.me/blog/id-diagram-6.webp)

See that? After we move `item 2` again, the free gap gets much smaller. Even when we start with a gap of 10, we soon run out of space for new items.

## Attempt 6: decimal numbers

This happens because integers are discrete. There is nothing between 0 and 1. Real numbers, on the other hand, are infinite. So why not use floats, the decimal numbers computers use?

Because floats do not solve the problem. They only delay it. You may get 10 to 50 more moves before you reach the limit of floating point precision. Math has infinite precision. A 64 bit float does not. We need a better way.

## Attempt 7: letters instead of numbers

Who said we must use numbers? Why not use strings? We can use letters and digits and sort them lexicographically, the way a dictionary sorts words.

![](https://storage.rolandoandrade.me/blog/id-diagram-7.webp)

It works, but a single character has the same limits as integers. The "gap" between "A" and "B" is fixed. We need finer steps.

This is where it gets interesting. With strings, we can add characters to create new "midpoints". "A" comes before "B", and "AA" falls between "A" and "B", as if they were fractions.

![](https://storage.rolandoandrade.me/blog/id-diagram-8.webp)

Awesome, right? By adding characters, we create gaps that are practically infinite. But practically infinite is not truly infinite. The strings can grow too long. In the end, you may hit a length limit or see performance drop.

We could rebalance the ranks from time to time, say every 12 hours. Rebalancing resets all positions so they have nice, wide gaps again. It works, but it needs a heavy batch job with many reads and writes.

## The winner: LexoRank buckets

The other solution, made popular by **Jira**, is **LexoRank** with buckets. A bucket is a prefix, a container for sorted elements. When one bucket runs out of gaps, you move the items to a fresh bucket with reset ranks. That handles the rebalancing without a big pause.

![](https://storage.rolandoandrade.me/blog/id-diagram-9.webp)

In the diagram, we start in Bucket 0. When a move causes a rank collision or leaves almost no gap, we move the affected elements (or all of them) to Bucket 1. That resets the spacing without downtime.

For my project, the bucket strategy was perfect. With 6 character strings, collisions are very rare. If one happens, it only affects that one list. For your project, you may mix approaches. Use buckets to fix problems right away, and periodic rebalancing for long term maintenance.

![Every approach from this post, side by side](https://storage.rolandoandrade.me/blog/storing-sortable-items-in-a-relational-database-comparison.webp)

## Pick your trade off

These are the approaches I explored while building Bezier. There is no single "best" solution. There is only the one that fits your limits and your use case.

* * *

If you enjoy the content, please don't hesitate to subscribe and leave a comment! I would love to connect with you and hear your thoughts on the topics I cover.

[Book a call with Rolando](https://rolandoandrade.me/pricing/)
