Skip to content

Commit

Permalink
Sort filenames naturally using CompareStringEx (#569)
Browse files Browse the repository at this point in the history
* Sort filenames naturally using StrCmpLogicalW

* Replace StrCmpLogicalW with CompareStringEx
  • Loading branch information
jeffsieu authored Apr 11, 2020
1 parent 7e24663 commit b984cab
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
1 change: 1 addition & 0 deletions Files/Files.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
<DependentUpon>ConfirmDeleteDialog.xaml</DependentUpon>
</Compile>
<Compile Include="Helpers\ItemsDataTemplateSelector.cs" />
<Compile Include="Helpers\NaturalStringComparer.cs" />
<Compile Include="UserControls\NavigationToolbar\INavigationToolbar.cs" />
<Compile Include="UserControls\NavigationToolbar\ModernNavigationToolbar.xaml.cs">
<DependentUpon>ModernNavigationToolbar.xaml</DependentUpon>
Expand Down
54 changes: 54 additions & 0 deletions Files/Helpers/NaturalStringComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Files.Helpers
{
internal static class SafeNativeMethods
{
public static readonly Int32 NORM_IGNORECASE = 0x00000001;
public static readonly Int32 NORM_IGNORENONSPACE = 0x00000002;
public static readonly Int32 NORM_IGNORESYMBOLS = 0x00000004;
public static readonly Int32 LINGUISTIC_IGNORECASE = 0x00000010;
public static readonly Int32 LINGUISTIC_IGNOREDIACRITIC = 0x00000020;
public static readonly Int32 NORM_IGNOREKANATYPE = 0x00010000;
public static readonly Int32 NORM_IGNOREWIDTH = 0x00020000;
public static readonly Int32 NORM_LINGUISTIC_CASING = 0x08000000;
public static readonly Int32 SORT_STRINGSORT = 0x00001000;
public static readonly Int32 SORT_DIGITSASNUMBERS = 0x00000008;

public static readonly String LOCALE_NAME_USER_DEFAULT = null;
public static readonly String LOCALE_NAME_INVARIANT = String.Empty;
public static readonly String LOCALE_NAME_SYSTEM_DEFAULT = "!sys-default-locale";

[DllImport("api-ms-win-core-string-l1-1-0 .dll", CharSet = CharSet.Unicode)]
public static extern Int32 CompareStringEx(
String localeName,
Int32 flags,
String str1,
Int32 count1,
String str2,
Int32 count2,
IntPtr versionInformation,
IntPtr reserved,
Int32 param
);
}

public class NaturalStringComparer : IComparer<object>
{
public int Compare(object a, object b)
{
return SafeNativeMethods.CompareStringEx(
SafeNativeMethods.LOCALE_NAME_USER_DEFAULT,
SafeNativeMethods.SORT_DIGITSASNUMBERS, // Add other flags if required.
a.ToString(),
a.ToString().Length,
b.ToString(),
b.ToString().Length,
IntPtr.Zero,
IntPtr.Zero,
0) - 2;
}
}
}
28 changes: 22 additions & 6 deletions Files/View Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ByteSizeLib;
using Files.Enums;
using Files.Helpers;
using Files.Interacts;
using Files.View_Models;
using Files.Views.Pages;
using Microsoft.Toolkit.Uwp.UI;
using Microsoft.UI.Xaml.Controls;
Expand Down Expand Up @@ -389,6 +389,7 @@ public void OrderFiles()

static object orderByNameFunc(ListedItem item) => item.ItemName;
Func<ListedItem, object> orderFunc = orderByNameFunc;
NaturalStringComparer naturalStringComparer = new NaturalStringComparer();
switch (DirectorySortOption)
{
case SortOption.Name:
Expand All @@ -412,22 +413,37 @@ public void OrderFiles()
List<ListedItem> orderedList;

if (DirectorySortDirection == SortDirection.Ascending)
ordered = _filesAndFolders.OrderBy(folderThenFileAsync).ThenBy(orderFunc);
{
if (DirectorySortOption == SortOption.Name)
ordered = _filesAndFolders.OrderBy(folderThenFileAsync).ThenBy(orderFunc, naturalStringComparer);
else
ordered = _filesAndFolders.OrderBy(folderThenFileAsync).ThenBy(orderFunc);
}
else
{
if (DirectorySortOption == SortOption.FileType)
ordered = _filesAndFolders.OrderBy(folderThenFileAsync).ThenByDescending(orderFunc);
{
if (DirectorySortOption == SortOption.Name)
ordered = _filesAndFolders.OrderBy(folderThenFileAsync).ThenByDescending(orderFunc, naturalStringComparer);
else
ordered = _filesAndFolders.OrderBy(folderThenFileAsync).ThenByDescending(orderFunc);
}
else
ordered = _filesAndFolders.OrderByDescending(folderThenFileAsync).ThenByDescending(orderFunc);
{
if (DirectorySortOption == SortOption.Name)
ordered = _filesAndFolders.OrderByDescending(folderThenFileAsync).ThenByDescending(orderFunc, naturalStringComparer);
else
ordered = _filesAndFolders.OrderByDescending(folderThenFileAsync).ThenByDescending(orderFunc);
}
}

// Further order by name if applicable
if (DirectorySortOption != SortOption.Name)
{
if (DirectorySortDirection == SortDirection.Ascending)
ordered = ordered.ThenBy(orderByNameFunc);
ordered = ordered.ThenBy(orderByNameFunc, naturalStringComparer);
else
ordered = ordered.ThenByDescending(orderByNameFunc);
ordered = ordered.ThenByDescending(orderByNameFunc, naturalStringComparer);
}
orderedList = ordered.ToList();
_filesAndFolders.Clear();
Expand Down

0 comments on commit b984cab

Please sign in to comment.