--- /Users/frank/tmp/carto-1.2.0/lib/carto/parser.js	2019-01-28 18:19:04
+++ lib/carto/parser.js	2025-05-12 11:06:20
@@ -244,6 +244,10 @@
 
             // Sort rules by specificity: this function expects selectors to be
             // split already.
+            // If the specifity is equal, the definition is compared by the elements
+            // array. Definitions with more elements are 'larger', and definitions
+            // with the same number of elements are sorted alphabetically.
+            // If the elements are also equal, the zoom level of the definition is compared
             //
             // Written to be used as a .sort(Function);
             // argument.
@@ -256,7 +260,22 @@
                 if (as[0] != bs[0]) return bs[0] - as[0];
                 if (as[1] != bs[1]) return bs[1] - as[1];
                 if (as[2] != bs[2]) return bs[2] - as[2];
-                return bs[3] - as[3];
+                if (bs[3] != as[3]) return bs[3] - as[3];
+
+                // The definition with the most elements is 'larger'
+                if (a.elements.length != b.elements.length) return b.elements.length - a.elements.length;
+
+                // Sort based on the alphabetic order of each element
+                for (var i = 0; i < a.elements.length; i++) {
+                    if (a.elements[i] != b.elements[i]) {
+                        return a.elements[i].value.localeCompare(b.elements[i].value);
+                    }
+                }
+
+                if (a.zoom != b.zoom) return b.zoom - a.zoom;
+
+                // Order is not defined
+                return 0;
             };
 
             return root;
