1

I am looking for material covering this idea:

Given a list-like data structure (e.g. database table) a sortKey property (column) should be introduced that

  1. reflects the desired sort order (ORDER BY sortKey)
  2. is unique within the list
  3. makes insertions at any position in the (sorted) list possible
  4. is immutable

Requirements 1-3 can be achieved with a integer sortKey, where with each insertion, all elements get potentially assigned a new sortKey.

Requirements 1-4 could be achieved with a tree'ish sortKey, e.g. a decimal type range (0.0, 1.0); each insertion would read the sortKey for its predecessor and successor and use sortKey(newItem) = ( sortKey(left) + sortKey(right) ) / 2

Is there a common term for this problem/solution, that I can lookup or can someone please point me to literatur.

Thanks!

Steve Oh
  • 1,149
  • 10
  • 18

1 Answers1

4

There isn't really a good solution that meets all of these requirements. (3) + (4) requires keys to be variable length, and an adversary can always find an insertion sequence that makes the length of keys proportional to the number of keys inserted.

The problem is called the "online list labeling problem" or the "order maintenance problem", and a google search for these terms will find various compromises that may or may not be useful to you.

https://en.wikipedia.org/wiki/Order-maintenance_problem

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87