SORT VARIABLES: Improve stability of sort.
authorBen Pfaff <blp@cs.stanford.edu>
Wed, 20 Sep 2017 15:36:18 +0000 (08:36 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Sat, 23 Sep 2017 17:49:37 +0000 (10:49 -0700)
The sort was being made "stable" by comparing variables' addresses in
memory.  There's nothing particularly predictable about that, especially
from one malloc implementation to another.  This commit fixes the stability
by comparing the variables' dictionary indexes instead.

Bug #52072.

src/language/dictionary/sort-variables.c

index 8c653af7248c1cf8470009e06d7c53dba483a129..aee341bda61770f633964c1ab493960c0366ed4a 100644 (file)
@@ -190,7 +190,11 @@ compare_vars (const void *a_, const void *b_, const void *c_)
 
   /* Make this a stable sort. */
   if (!retval)
-    retval = a < b ? -1 : a > b;
+    {
+      size_t a_index = var_get_dict_index (a);
+      size_t b_index = var_get_dict_index (b);
+      retval = a_index < b_index ? -1 : a_index > b_index;
+    }
 
   if (c->descending)
     retval = -retval;