Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sorting.Test/NaturalStringComparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
28 changes: 26 additions & 2 deletions Sorting/NaturalStringComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/// Regular expression to match alphanumeric chunks in a string.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "SYSLIB1045:Convert to 'GeneratedRegexAttribute'.", Justification = "<Pending>")]
private static Regex CreateNaturalChunkRegex() => new(@"(\d+)|(\D+)");

Check warning on line 16 in Sorting/NaturalStringComparer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 16 in Sorting/NaturalStringComparer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 16 in Sorting/NaturalStringComparer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 16 in Sorting/NaturalStringComparer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

/// <summary>
/// Compares two strings using natural sorting, where embedded numbers are compared as numeric values.
Expand All @@ -25,7 +25,7 @@
/// Zero if x equals y.
/// Greater than zero if x is greater than y.
/// </returns>
public int Compare(string? x, string? y)

Check warning on line 28 in Sorting/NaturalStringComparer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 28 in Sorting/NaturalStringComparer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 28 in Sorting/NaturalStringComparer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 28 in Sorting/NaturalStringComparer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 28 in Sorting/NaturalStringComparer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 28 in Sorting/NaturalStringComparer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.
{
if (x == null && y == null)
{
Expand Down Expand Up @@ -58,9 +58,9 @@
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;
Expand All @@ -79,4 +79,28 @@
// 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);
}
}