Skip to content

Commit acfabea

Browse files
committed
Fix: files sorting problems with #-# numbered files
1 parent f4680b5 commit acfabea

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/Files.App/Helpers/NaturalStringComparer.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public static int Compare(ReadOnlySpan<char> x, ReadOnlySpan<char> y, StringComp
6666

6767
for (var i = 0; i < length; i++)
6868
{
69+
while (i < x.Length && i < y.Length && IsIgnorableSeparator(x, i) && IsIgnorableSeparator(y, i))
70+
i++;
71+
72+
if (i >= x.Length || i >= y.Length) break;
73+
6974
if (char.IsDigit(x[i]) && char.IsDigit(y[i]))
7075
{
7176
var xOut = GetNumber(x.Slice(i), out var xNumAsSpan);
@@ -90,6 +95,18 @@ public static int Compare(ReadOnlySpan<char> x, ReadOnlySpan<char> y, StringComp
9095
return x.Length.CompareTo(y.Length);
9196
}
9297

98+
private static bool IsIgnorableSeparator(ReadOnlySpan<char> span, int index)
99+
{
100+
if (span[index] != '-' && span[index] != '_') return false;
101+
102+
// Check bounds before accessing span[index + 1] or span[index - 1]
103+
if (index == 0) return span.Length > 1 && char.IsLetterOrDigit(span[index + 1]);
104+
if (index == span.Length - 1) return span.Length > 1 && char.IsLetterOrDigit(span[index - 1]);
105+
106+
return char.IsLetterOrDigit(span[index - 1]) && char.IsLetterOrDigit(span[index + 1]);
107+
}
108+
109+
93110
private static ReadOnlySpan<char> GetNumber(ReadOnlySpan<char> span, out ReadOnlySpan<char> number)
94111
{
95112
var i = 0;

0 commit comments

Comments
 (0)