+/* 1 if x has lower priority than y. */
+static int node_lessthan(struct node *x, struct node *y)/*{{{*/
+{
+  if (x->priority < y->priority)
+    return 1;
+  if (x->priority > y->priority)
+    return 0;
+  if (x->required_by == 0 && y->required_by == 0)
+    return (x->idx > y->idx);
+  if (y->required_by == 0)
+    return 1;
+  if (x->required_by == 0)
+    return 0;
+  return x->required_by < y->required_by;
+}
+/*}}}*/
+static void sort_chain(struct links *x)/*{{{*/
+{
+  struct links new;
+  struct node *y, *ynext, *yprev;
+  int idx;
+  
+  new.next = NULL;
+  new.prev = NULL;
+  
+  /* Stupidsort. */
+  for (y = x->next, idx = 1; y != (struct node *) x; y = ynext, ++idx) {
+    /* y is now the current node; go insert it into its rightful place in
+     * the new chain.  */
+    struct node **nextp = &(new.next);
+    struct node *ins;
+    for (ins = new.next; ins && node_lessthan(ins, y); nextp = &(ins->chain.next), ins = ins->chain.next)
+      ;
+    y->chain.next->chain.prev = y->chain.prev;
+    y->chain.prev->chain.next = y->chain.next;
+    ynext = y->chain.next;
+    y->chain.next = *nextp;
+    *nextp = y;
+    y->idx = idx;
+  }
+  
+  /* Now clean up the new links. */
+  yprev = (struct node *)x;
+  for (y = new.next; y; yprev = y, y = y->chain.next) {
+    y->chain.prev = yprev;
+  }
+  yprev->chain.next = (struct node *)x;
+  new.prev = y;
+  
+  if (new.next == NULL) {
+    x->next = (struct node *)x;
+    x->prev = (struct node *)x;
+  } else {
+    x->next = new.next;
+    x->prev = new.prev;
+  }
+}
+/*}}}*/