本文介绍了在C#中的自然排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人有一个很好的资源或为的FileInfo
阵列提供一个自然顺序排序的样品在C#中?我实施我的排序的IComparer
接口。
Anyone have a good resource or provide a sample of a natural order sort in C# for an FileInfo
array? I am implementing the IComparer
interface in my sorts.
推荐答案
做最简单的事情就是P /调用Windows中内置的功能,并在使用它作为比较函数你的的IComparer
:
The easiest thing to do is just P/Invoke the built-in function in Windows, and use it as the comparison function in your IComparer
:
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
private static extern int StrCmpLogicalW(string psz1, string psz2);
迈克尔·卡普兰具有该功能的工作原理这里一些例子,这是变化为Vista做,使之更加直观。这个函数的加方是它会产生相同的行为Windows版本在其上运行,但是这并不意味着它的Windows版本之间的不同,所以你需要考虑这是否对你是一个问题。
So a complete implementation would be something like:
[SuppressUnmanagedCodeSecurity]
internal static class SafeNativeMethods
{
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
public static extern int StrCmpLogicalW(string psz1, string psz2);
}
public sealed class NaturalStringComparer : IComparer<string>
{
public int Compare(string a, string b)
{
return SafeNativeMethods.StrCmpLogicalW(a, b);
}
}
public sealed class NaturalFileInfoNameComparer : IComparer<FileInfo>
{
public int Compare(FileInfo a, FileInfo b)
{
return SafeNativeMethods.StrCmpLogicalW(a.Name, b.Name);
}
}
这篇关于在C#中的自然排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!