-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sort filenames naturally using CompareStringEx (#569)
* Sort filenames naturally using StrCmpLogicalW * Replace StrCmpLogicalW with CompareStringEx
- Loading branch information
Showing
3 changed files
with
77 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters