/*
  * A "variable space" is represented by an otherwise-unused struct _variable
  * that serves as list header.
+ *
+ * The list entries are kept in name order (according to strcmp).  This
+ * is mainly to make the results of PrintVariables() more pleasing.
  */
 VariableSpace
 CreateVariableSpace(void)
 
    for (current = space->next; current; current = current->next)
    {
-       if (strcmp(current->name, name) == 0)
+       int         cmp = strcmp(current->name, name);
+
+       if (cmp == 0)
        {
            /* this is correct answer when value is NULL, too */
            return current->value;
        }
+       if (cmp > 0)
+           break;              /* it's not there */
    }
 
    return NULL;
         current;
         previous = current, current = current->next)
    {
-       if (strcmp(current->name, name) == 0)
+       int         cmp = strcmp(current->name, name);
+
+       if (cmp == 0)
        {
            /*
             * Found entry, so update, unless assign hook returns false.
 
            return confirmed;
        }
+       if (cmp > 0)
+           break;              /* it's not there */
    }
 
    /* not present, make new entry ... unless we were asked to delete */
        current->value = pg_strdup(value);
        current->substitute_hook = NULL;
        current->assign_hook = NULL;
-       current->next = NULL;
+       current->next = previous->next;
        previous->next = current;
    }
    return true;
         current;
         previous = current, current = current->next)
    {
-       if (strcmp(current->name, name) == 0)
+       int         cmp = strcmp(current->name, name);
+
+       if (cmp == 0)
        {
            /* found entry, so update */
            current->substitute_hook = shook;
                (void) (*ahook) (current->value);
            return;
        }
+       if (cmp > 0)
+           break;              /* it's not there */
    }
 
    /* not present, make new entry */
    current->value = NULL;
    current->substitute_hook = shook;
    current->assign_hook = ahook;
-   current->next = NULL;
+   current->next = previous->next;
    previous->next = current;
    if (shook)
        current->value = (*shook) (current->value);