Skip to content

Fix: Fixed sorting issues with #-# numbered files #17012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 8, 2025
Merged
Changes from 2 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
106 changes: 79 additions & 27 deletions src/Files.App/Helpers/NaturalStringComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,86 @@ public int Compare(ReadOnlyMemory<char> x, ReadOnlyMemory<char> y)
}

public static int Compare(ReadOnlySpan<char> x, ReadOnlySpan<char> y, StringComparison stringComparison)
{
var length = Math.Min(x.Length, y.Length);

for (var i = 0; i < length; i++)
{
if (char.IsDigit(x[i]) && char.IsDigit(y[i]))
{
var xOut = GetNumber(x.Slice(i), out var xNumAsSpan);
var yOut = GetNumber(y.Slice(i), out var yNumAsSpan);

var compareResult = CompareNumValues(xNumAsSpan, yNumAsSpan);

if (compareResult != 0) return compareResult;
{
// Handle file extensions specially
int xExtPos = GetExtensionPosition(x);
int yExtPos = GetExtensionPosition(y);

// If both have extensions, compare the names first
if (xExtPos >= 0 && yExtPos >= 0)
{
var xName = x.Slice(0, xExtPos);
var yName = y.Slice(0, yExtPos);

int nameCompare = CompareWithoutExtension(xName, yName, stringComparison);
if (nameCompare != 0)
return nameCompare;

// If names match, compare extensions
return x.Slice(xExtPos).CompareTo(y.Slice(yExtPos), stringComparison);
}

// Original comparison logic for non-extension cases
return CompareWithoutExtension(x, y, stringComparison);
}

private static int CompareWithoutExtension(ReadOnlySpan<char> x, ReadOnlySpan<char> y, StringComparison stringComparison)
{
var length = Math.Min(x.Length, y.Length);

for (var i = 0; i < length; i++)
{
while (i < x.Length && i < y.Length && IsIgnorableSeparator(x, i) && IsIgnorableSeparator(y, i))
i++;

if (i >= x.Length || i >= y.Length) break;

if (char.IsDigit(x[i]) && char.IsDigit(y[i]))
{
var xOut = GetNumber(x.Slice(i), out var xNumAsSpan);
var yOut = GetNumber(y.Slice(i), out var yNumAsSpan);

var compareResult = CompareNumValues(xNumAsSpan, yNumAsSpan);

if (compareResult != 0) return compareResult;

i = -1;
length = Math.Min(xOut.Length, yOut.Length);

x = xOut;
y = yOut;
continue;
}

var charCompareResult = x.Slice(i, 1).CompareTo(y.Slice(i, 1), stringComparison);
if (charCompareResult != 0) return charCompareResult;
}

return x.Length.CompareTo(y.Length);
}

private static int GetExtensionPosition(ReadOnlySpan<char> text)
{
// Find the last period that's not at the beginning
for (int i = text.Length - 1; i > 0; i--)
{
if (text[i] == '.')
return i;
}
return -1;
}

private static bool IsIgnorableSeparator(ReadOnlySpan<char> span, int index)
{
if (span[index] != '-' && span[index] != '_') return false;

// Check bounds before accessing span[index + 1] or span[index - 1]
if (index == 0) return span.Length > 1 && char.IsLetterOrDigit(span[index + 1]);
if (index == span.Length - 1) return span.Length > 1 && char.IsLetterOrDigit(span[index - 1]);

return char.IsLetterOrDigit(span[index - 1]) && char.IsLetterOrDigit(span[index + 1]);
}

i = -1;
length = Math.Min(xOut.Length, yOut.Length);

x = xOut;
y = yOut;
continue;
}

var charCompareResult = x.Slice(i, 1).CompareTo(y.Slice(i, 1), stringComparison);
if (charCompareResult != 0) return charCompareResult;
}

return x.Length.CompareTo(y.Length);
}

private static ReadOnlySpan<char> GetNumber(ReadOnlySpan<char> span, out ReadOnlySpan<char> number)
{
Expand Down
Loading