# If you're not sure how to sort a list or grid—seriate it!

["Getting Things in Order: An Introduction to the R Package seriation"](https://www.jstatsoft.org/article/view/v025i03), Hahsler et al 2008:

>  [**Seriation**](https://en.wikipedia.org/wiki/Seriation_(archaeology)) [or ["ordination"](https://en.wikipedia.org/wiki/Ordination_(statistics))], i.e., finding a suitable linear order for a set of objects given data and a loss or merit function, is a basic problem in data analysis. Caused by the problem's combinatorial nature, it is hard to solve for all but very small sets. Nevertheless, both exact solution methods and heuristics are available. 
>
> In this paper we present [the package `seriation`](https://cran.r-project.org/web/packages/seriation/index.html) which provides an infrastructure for seriation with R. The infrastructure comprises data structures to represent linear orders as permutation vectors, a wide array of seriation methods using a consistent interface, a method to calculate the value of various loss and merit functions, and several visualization techniques which build on seriation. 
>
> To illustrate how easily the package can be applied for a variety of applications, a comprehensive collection of examples is presented.

---

Have you ever wondered how to sort a list or a folder of files where no strict sorting comparison operator like 'newer than' is quite right? It turns out that it is perfectly possible to loosen the definition of 'sorting' to something more approximate like 'try to minimize how different each item is from the next one'; this approximate or generalized sorting is called 'seriation'. It is obscure (I had never heard of the term until a year ago or so), but highly useful: it works for everything from seriating [Egyptian graves by rough burial time](https://en.wikipedia.org/wiki/Seriation_(archaeology)#History) to organizing tag entries by topic. Since we now have neural embeddings for just about every modality there is, that means you can seriate anything. 

What might you use it for? I use it on Gwern.net ([background](https://gwern.net/doc/cs/algorithm/sorting/seriation/index)) to organize the 'similar links' recommendations in a way much smarter than the naive _k_-NN embedding distance retrieval approach. If you just sort them by 'distance', it is mostly meaningless and produces a jumble (see for example any algorithmic set of recommendations, like YouTube video lists - if I open a cat video, I see cat/anime/Touhou/cat/CS/music/cat/...); if you seriate them, however, suddenly you see clear clusters/topics emerge out of the chaos, and it's easier to skim the list. Because it works so well, and is so simple to implement (simple greedy distance minimization heuristic worked surprisingly well for my use-case, no need for [TSP](https://en.wikipedia.org/wiki/Travelling_salesman_problem) solvers), I initially called it ["sort by magic"](https://gwern.net/design#sort-by-magic). This could be used to seriate LW2 tag-entries by something more relevant than either just date or upvotes. It could also be used to present the tags themselves as a big 2D grid. (A tag embedding is the average of all its members' embeddings. Then you just seriate them as if they were normal tagged items.) You can do further nice tricks with it, like infer the number of clusters/topics by where distances spike in size, and label them automatically with a LLM. I sometimes seriate regular lists of text in my writing, where I can't come up with something meaningful.

Seriation also works beautifully for photographs or other big jumbles of images, where usually there is no way to 'sort' them that matters. (The filenames may be hash gibberish, and the file-created times arbitrary, or indicate nothing more than when you happened to download a file.) The practice of 'sorting by color' can be a simple form of seriation, and better than nothing.

I've also played around with an OA API LLM script for doing seriation on short natural language text lists, which could be used to automatically seriate lists in anything you write, or which could be used to clean up messy notes. (For example, you could seriate your chaotic incoherent notes, then tell a LLM to rewrite your notes strictly line by line, and then, with the organization and grammar/spelling all cleaned up, start working on it yourself.) It works but the data formatting issues are a bit tricky, so I think I may have to scrap my little prototype and start over with a JSON/'function-calling'-style approach, which I am not too familiar with. But it's not hard, a GPT-4o-scale LLM understands pretty well a prompt like "Reorder them to group similar items, but do NOT rename, add, or remove any items.", so you can easily make your own tool.

---

Keep this in mind the next time you see a list or a grid anywhere, where there's not an *obviously* correct way to sort it: it doesn't have to be sorted in a dumb way or left sorted at random, when it could be... *seriated*!