我需要对文件名进行排序,如下所示:1.log,2.log,10.log
但是当我使用OrderBy(fn => fn)时,它会将它们排序为:
1.log,10.log,2.log
我显然知道这可以通过写另一个比较者来完成,但是是否有一个更简单的方法来从字典排列顺序转变为自然排序?
编辑:目的是获得与Windows资源管理器中选择“按名称排序”相同的顺序.
解决方法
您可以使用Win32
您将使用P / Invoke:
CompareStringEx功能.在Windows 7上,它支持您需要的排序.
您将使用P / Invoke:
static readonly Int32 norM_IGnorECASE = 0x00000001;
static readonly Int32 norM_IGnorENONSPACE = 0x00000002;
static readonly Int32 norM_IGnorESYMBOLS = 0x00000004;
static readonly Int32 LINGUISTIC_IGnorECASE = 0x00000010;
static readonly Int32 LINGUISTIC_IGnorEDIACRITIC = 0x00000020;
static readonly Int32 norM_IGnorEKANATYPE = 0x00010000;
static readonly Int32 norM_IGnorEWIDTH = 0x00020000;
static readonly Int32 norM_LINGUISTIC_CASING = 0x08000000;
static readonly Int32 SORT_STRINGSORT = 0x00001000;
static readonly Int32 SORT_DIGITSASNUMBERS = 0x00000008;
static readonly String LOCALE_NAME_USER_DEFAULT = null;
static readonly String LOCALE_NAME_INVARIANT = String.Empty;
static readonly String LOCALE_NAME_SYstem_DEFAULT = "!sys-default-locale";
[DllImport("kernel32.dll",CharSet = CharSet.Unicode)]
static extern Int32 CompareStringEx(
String localeName,Int32 flags,String str1,Int32 count1,String str2,Int32 count2,IntPtr versioninformation,IntPtr reserved,Int32 param
);
然后,您可以创建一个使用SORT_DIGITSASNUMBERS标志的IComparer:
class LexicographicalComparer : IComparer<String> {
readonly String locale;
public LexicographicalComparer() : this(CultureInfo.CurrentCulture) { }
public LexicographicalComparer(CultureInfo cultureInfo) {
if (cultureInfo.IsNeutralCulture)
this.locale = LOCALE_NAME_INVARIANT;
else
this.locale = cultureInfo.Name;
}
public Int32 Compare(String x,String y) {
// CompareStringEx return 1,2,or 3. Subtract 2 to get the return value.
return CompareStringEx(
this.locale,SORT_DIGITSASNUMBERS,// Add other flags if required.
x,x.Length,y,y.Length,IntPtr.Zero,0) - 2;
}
}
然后,您可以在各种排序API中使用IComparer:
var names = new [] { "2.log","10.log","1.log" };
var sortednames = names.OrderBy(s => s,new LexicographicalComparer());
您也可以使用StrCmpLogicalW这是Windows资源管理器使用的功能.自Windows XP以来一直可用
[DllImport("shlwapi.dll",CharSet = CharSet.Unicode)]
static extern Int32 StrCmpLogical(String x,String y);
class LexicographicalComparer : IComparer<String> {
public Int32 Compare(String x,String y) {
return StrCmpLogical(x,y);
}
}
比较简单,但对比较少.