Fix length-based comparison issue

This commit is contained in:
Matt Nadareski
2025-05-12 08:09:59 -04:00
parent 4f56d716d4
commit 76e13a47ec
2 changed files with 32 additions and 4 deletions

View File

@@ -8,7 +8,7 @@ namespace SabreTools.Matching.Test.Compare
public class NaturalComparerTests
{
[Fact]
public void NaturalComparer_ListSortTest()
public void NaturalComparer_ListSort_Numeric()
{
// Setup arrays
string[] sortable = ["0", "100", "5", "2", "1000"];
@@ -22,7 +22,21 @@ namespace SabreTools.Matching.Test.Compare
}
[Fact]
public void NaturalReversedComparer_ListSortTest()
public void NaturalComparer_ListSort_Mixed()
{
// Setup arrays
string[] sortable = ["b3b", "c", "b", "a", "a1"];
string[] expected = ["a", "a1", "b", "b3b", "c"];
// Run sorting on array
Array.Sort(sortable, new NaturalComparer());
// Check the output
Assert.True(sortable.SequenceEqual(expected));
}
[Fact]
public void NaturalReversedComparer_ListSort_Numeric()
{
// Setup arrays
string[] sortable = ["0", "100", "5", "2", "1000"];
@@ -34,5 +48,19 @@ namespace SabreTools.Matching.Test.Compare
// Check the output
Assert.True(sortable.SequenceEqual(expected));
}
[Fact]
public void NaturalReversedComparer_ListSort_Mixed()
{
// Setup arrays
string[] sortable = ["b3b", "c", "b", "a", "a1"];
string[] expected = ["c", "b3b", "b", "a1", "a"];
// Run sorting on array
Array.Sort(sortable, new NaturalReversedComparer());
// Check the output
Assert.True(sortable.SequenceEqual(expected));
}
}
}

View File

@@ -66,9 +66,9 @@ namespace SabreTools.Matching.Compare
return PartCompare(x1[i], y1[i]);
}
if (y1.Length > x1.Length)
if (x1.Length > y1.Length)
return 1;
else if (x1.Length > y1.Length)
else if (y1.Length > x1.Length)
return -1;
else
return x.CompareTo(y);