The natural sorting of the FileInfo array in C#realized
Question:
How to use the iComparer interface in C#to implement the natural sort of the FileInfo object?
Solution:
In order to achieve natural sorting, we can use the built -in function Strcmplogicalw in Windows API.
Implement:
First of all, create an internal class SafenatingMethods to suppress the non -host code security warning:
Next, define a naturstringcomparer class to implement iComparerInterface:
<code class="language-csharp">[SuppressUnmanagedCodeSecurity] internal static class SafeNativeMethods { [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)] public static extern int StrCmpLogicalW(string psz1, string psz2); }</code>
Finally, create a naturFileInfonamecomparer class, using the natural order order to compare the FileInfo object:
<code class="language-csharp">public sealed class NaturalStringComparer : IComparer<string> { public int Compare(string a, string b) { return SafeNativeMethods.StrCmpLogicalW(a, b); } }</code>
The above is the detailed content of How to Implement Natural Sort Order for FileInfo Arrays in C#?. For more information, please follow other related articles on the PHP Chinese website!