From 61cbf572bf85bc6079b3dae0aa2d50f6349b3e6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20R=C3=BCter?= Date: Thu, 5 Feb 2026 16:46:08 +0000 Subject: [PATCH] Change Symbol order to have uppercase names before lowercase ones. This is the canonical order in WL. --- mathics/core/symbols.py | 6 ++++-- test/builtin/statistics/test_orderstats.py | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/mathics/core/symbols.py b/mathics/core/symbols.py index 0426e0e3e..b77b07dc7 100644 --- a/mathics/core/symbols.py +++ b/mathics/core/symbols.py @@ -562,9 +562,11 @@ def element_order(self) -> tuple: if self.is_numeric() else BASIC_EXPRESSION_ELT_ORDER ), - Monomial({self.name: 1}), + Monomial({str.lower(self.name): 1}), + # The canonical in for WL is to sort by lowercase first. Hence we must swap the case. + Monomial({str.swapcase(self.name): 1}), 0, - self.name, + str.lower(self.name), 1, ) diff --git a/test/builtin/statistics/test_orderstats.py b/test/builtin/statistics/test_orderstats.py index 52cd46169..d18197b7b 100644 --- a/test/builtin/statistics/test_orderstats.py +++ b/test/builtin/statistics/test_orderstats.py @@ -11,6 +11,10 @@ def test_canonical_sort(): "{ByteArray[<1>], F[2]}", hold_expected=True, ) + check_evaluation( + "Sort[{Mathics,Math,ics,mathics}]", + "{ics, Math, mathics, Mathics}", + ) check_evaluation( r"Sort[Table[IntegerDigits[2^n], {n, 10}]]", r"{{2}, {4}, {8}, {1, 6}, {3, 2}, {6, 4}, {1, 2, 8}, {2, 5, 6}, {5, 1, 2}, {1, 0, 2, 4}}",