tompazourek / NaturalSort.Extension

🔀 Extension method for StringComparison that adds support for natural sorting (e.g. "abc1", "abc2", "abc10" instead of "abc1", "abc10", "abc2").
MIT License
169 stars 13 forks source link

Wrong sorting #1

Closed antonioborondo closed 5 years ago

antonioborondo commented 5 years ago

The following list of files ordered by name in Windows explorer:

1.txt
1a.txt
2.txt
11.txt
a.txt
a1.txt
a2.txt
a11.txt
aa.txt
b.txt
ba.txt
bb.txt

Are sorted differently with NaturalSort.Extension:

1.txt
1a.txt
2.txt
11.txt
a1.txt
a2.txt
a11.txt
a.txt <- Wrong position
aa.txt
b.txt
ba.txt
bb.txt

Tested with NaturalSort.Extension v1.0.2 via NuGet.

tompazourek commented 5 years ago

Hi Antonio,

Thanks for opening an issue here.

The library doesn't claim to match 1:1 the behavior of Windows Explorer, so it's expected that there might be some differences. However, I also think that the first ordering as done by Windows Explorer might be a better result and more expected. So it might be good to alter the behavior a bit.

I'll try to check this out to see how my algorithm can be tweaked when I find some free time some day.

If you want to make a PR yourself, feel free to do so, the entire source code is very small: https://github.com/tompazourek/NaturalSort.Extension/blob/master/src/NaturalSort.Extension/StringComparerNaturalSortExtension.cs

tompazourek commented 5 years ago

I have rewritten the sorting algorithm so that it would work a bit differently.

I released the changes as version 2.0.0, it should be available on NuGet any moment now (once their validation and indexing finishes).

Your example is now one of the test cases: https://github.com/tompazourek/NaturalSort.Extension/blob/master/tests/NaturalSort.Extension.Tests/NaturalSortComparerTests.cs#L32-L33

Closing this. Thanks!

antonioborondo commented 5 years ago

Hi Tom, I just updated my app (https://github.com/antonioborondo/batch_file_renamer) to use the new version of your library and it works perfect. Thank you so much!

tompazourek commented 5 years ago

@antonioborondo Great, I'm glad to have helped!