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
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
30 changes: 30 additions & 0 deletions .github/NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -674,3 +674,33 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
```

## NaturalStringComparer

**Source**: [https://github.com/GihanSoft/NaturalStringComparer](https://github.com/GihanSoft/NaturalStringComparer)

### License

```
MIT License

Copyright (c) 2018 Mohammad Babayi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
107 changes: 80 additions & 27 deletions src/Files.App/Helpers/NaturalStringComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Files.App.Helpers
{
// Credit: https://github.com/GihanSoft/NaturalStringComparer
public sealed class NaturalStringComparer
{
public static IComparer<object> GetForProcessor()
Expand Down Expand Up @@ -61,34 +62,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
1 change: 1 addition & 0 deletions src/Files.App/ViewModels/Settings/AboutViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public AboutViewModel()
new ("https://github.com/PowerShell/MMI", "MMI"),
new ("https://github.com/microsoft/CsWin32", "CsWin32"),
new ("https://github.com/microsoft/CsWinRT", "CsWinRT"),
new ("https://github.com/GihanSoft/NaturalStringComparer", "NaturalStringComparer"),
];

CopyAppVersionCommand = new RelayCommand(CopyAppVersion);
Expand Down
Loading