From 87714a71d65f53dc20f0373b537fe3ba47786d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6ran=20W?= Date: Tue, 12 Aug 2025 16:52:35 +0200 Subject: [PATCH] Make NumericSort use InvariantCulture instead of Ordinal comparison Ordinal comparison sorts non-alphanumeric chars (like underscore etc) differently than expected (i.e compared to filesystem sorting etc). InvariantCulture comparison corrects this, without imposing cultural differences. (A possible alternative would be CurrentCulture comparison.) The earlier code, when comparing a single digit-char against non-digit-char, used char.CompareTo() where the comparison method can not be specified. To fix this, we now use the same Comparer for all non-all-digits string comparisons. --- src/Models/NumericSort.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Models/NumericSort.cs b/src/Models/NumericSort.cs index baaf3da4f..433a921bd 100644 --- a/src/Models/NumericSort.cs +++ b/src/Models/NumericSort.cs @@ -6,6 +6,8 @@ public static class NumericSort { public static int Compare(string s1, string s2) { + var comparer = StringComparer.InvariantCultureIgnoreCase; + int len1 = s1.Length; int len2 = s2.Length; @@ -20,7 +22,7 @@ public static int Compare(string s1, string s2) bool isDigit1 = char.IsDigit(c1); bool isDigit2 = char.IsDigit(c2); if (isDigit1 != isDigit2) - return c1.CompareTo(c2); + return comparer.Compare(c1.ToString(), c2.ToString()); int subLen1 = 1; while (marker1 + subLen1 < len1 && char.IsDigit(s1[marker1 + subLen1]) == isDigit1) @@ -40,7 +42,7 @@ public static int Compare(string s1, string s2) if (isDigit1) result = (subLen1 == subLen2) ? string.CompareOrdinal(sub1, sub2) : (subLen1 - subLen2); else - result = string.Compare(sub1, sub2, StringComparison.OrdinalIgnoreCase); + result = comparer.Compare(sub1, sub2); if (result != 0) return result;