From 0b2f81631e518610633f5e931592324b1f40c8d3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 13 Sep 2026 11:42:21 +0000 Subject: [PATCH 1/2] Initial plan From 36983e322c10ad4af07494e87d2842be93f022d0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 13 Sep 2026 11:44:19 +0000 Subject: [PATCH 2/2] Fix natural sort for numeric chunks beyond Int32 range Co-authored-by: matt-edmondson <19528727+matt-edmondson@users.noreply.github.com> --- Sorting.Test/NaturalStringComparerTests.cs | 3 +++ Sorting/NaturalStringComparer.cs | 28 ++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Sorting.Test/NaturalStringComparerTests.cs b/Sorting.Test/NaturalStringComparerTests.cs index 18402b3..9ff08fa 100644 --- a/Sorting.Test/NaturalStringComparerTests.cs +++ b/Sorting.Test/NaturalStringComparerTests.cs @@ -117,6 +117,9 @@ public void Compare_LargeNumbers_HandledCorrectly() { // Should handle numbers within integer range Assert.IsLessThan(0, _comparer.Compare("file1000000", "file9999999")); + + // Should handle numbers beyond Int32 range + Assert.IsLessThan(0, _comparer.Compare("file9999999999", "file10000000000")); } [TestMethod] diff --git a/Sorting/NaturalStringComparer.cs b/Sorting/NaturalStringComparer.cs index d79a34b..361844d 100644 --- a/Sorting/NaturalStringComparer.cs +++ b/Sorting/NaturalStringComparer.cs @@ -58,9 +58,9 @@ public int Compare(string? x, string? y) Match yMatch = yMatches[j++]; // If both chunks are numeric, compare them as numbers - if (int.TryParse(xMatch.Value, out int xNum) && int.TryParse(yMatch.Value, out int yNum)) + if (char.IsDigit(xMatch.Value[0]) && char.IsDigit(yMatch.Value[0])) { - int numComparison = xNum.CompareTo(yNum); + int numComparison = CompareNumericChunks(xMatch.Value, yMatch.Value); if (numComparison != 0) { return numComparison; @@ -79,4 +79,28 @@ public int Compare(string? x, string? y) // If we've exhausted one sequence but not the other, the shorter one comes first return xMatches.Length.CompareTo(yMatches.Length); } + + private static int CompareNumericChunks(string xChunk, string yChunk) + { + string xTrimmed = xChunk.TrimStart('0'); + string yTrimmed = yChunk.TrimStart('0'); + + if (xTrimmed.Length == 0) + { + xTrimmed = "0"; + } + + if (yTrimmed.Length == 0) + { + yTrimmed = "0"; + } + + int lengthComparison = xTrimmed.Length.CompareTo(yTrimmed.Length); + if (lengthComparison != 0) + { + return lengthComparison; + } + + return string.Compare(xTrimmed, yTrimmed, StringComparison.Ordinal); + } }