Nor*_*an 16 .net c# vb.net arrays sorting
我正在自杀并脱水试图让这个数组排序.
我有一个包含生成的目录的数组;
Dim Folders()As String = Directory.GetDirectories(RootPath)
我需要对它们进行排序,以便它们在win7/vista中的Windows资源管理器中显示. - 文件夹名称的数字和字母顺序.
文件夹名称包含字母和数字,有时仅包含字母或仅包含数字.
简单的Array.Sort(文件夹)结果
C:\inetpub\wwwroot\rootpath\1
C:\inetpub\wwwroot\rootpath\10
C:\inetpub\wwwroot\rootpath\100
C:\inetpub\wwwroot\rootpath\1004
C:\inetpub\wwwroot\rootpath\101
C:\inetpub\wwwroot\rootpath\11
C:\inetpub\wwwroot\rootpath\12
C:\inetpub\wwwroot\rootpath\2
C:\inetpub\wwwroot\rootpath\3
C:\inetpub\wwwroot\rootpath\4
C:\inetpub\wwwroot\rootpath\5
C:\inetpub\wwwroot\rootpath\6
C:\inetpub\wwwroot\rootpath\7
C:\inetpub\wwwroot\rootpath\8
C:\inetpub\wwwroot\rootpath\87skjnd
C:\inetpub\wwwroot\rootpath\89sdf93kmw3
C:\inetpub\wwwroot\rootpath\9
C:\inetpub\wwwroot\rootpath\ad
C:\inetpub\wwwroot\rootpath\bin
C:\inetpub\wwwroot\rootpath\dark
C:\inetpub\wwwroot\rootpath\erk
C:\inetpub\wwwroot\rootpath\jkh23978yoaslkd3
C:\inetpub\wwwroot\rootpath\lk2309as
C:\inetpub\wwwroot\rootpath\work
C:\inetpub\wwwroot\rootpath\zone
Run Code Online (Sandbox Code Playgroud)
我想拥有的(以及Windows资源管理器显示的内容)是......
C:\inetpub\wwwroot\rootpath\1
C:\inetpub\wwwroot\rootpath\2
C:\inetpub\wwwroot\rootpath\3
C:\inetpub\wwwroot\rootpath\4
C:\inetpub\wwwroot\rootpath\5
C:\inetpub\wwwroot\rootpath\6
C:\inetpub\wwwroot\rootpath\7
C:\inetpub\wwwroot\rootpath\8
C:\inetpub\wwwroot\rootpath\9
C:\inetpub\wwwroot\rootpath\10
C:\inetpub\wwwroot\rootpath\11
C:\inetpub\wwwroot\rootpath\12
C:\inetpub\wwwroot\rootpath\87skjnd
C:\inetpub\wwwroot\rootpath\89sdf93kmw3
C:\inetpub\wwwroot\rootpath\100
C:\inetpub\wwwroot\rootpath\101
C:\inetpub\wwwroot\rootpath\1004
C:\inetpub\wwwroot\rootpath\ad
C:\inetpub\wwwroot\rootpath\bin
C:\inetpub\wwwroot\rootpath\dark
C:\inetpub\wwwroot\rootpath\erk
C:\inetpub\wwwroot\rootpath\jkh23978yoaslkd3
C:\inetpub\wwwroot\rootpath\lk2309as
C:\inetpub\wwwroot\rootpath\work
C:\inetpub\wwwroot\rootpath\zone
Run Code Online (Sandbox Code Playgroud)
我用Google搜索并发现需要编写一个使用IComparable对类进行排序的类.作为超级新手...我真的不知道如何做到这一点.我看过的大多数例子都有多维数组和键:S ......
如果排序可以应用于文件名数组(而不是foldernames)或包含文件夹和文件的数组,那将会更好......在这种情况下,排序的文件夹显示在顶部,排序的文件显示在下面......这是甚至可能?
任何帮助都将受到极大关注......:谢谢.
Jos*_*osh 25
您需要实现IComparer,而不是创建实现IComparable的类.不同之处在于IComparer具有比较两个对象所必需的"知识",而IComparable则由知道如何将自身与其他东西进行比较的类实现.
Windows资源管理器对文件名进行排序的方式是使用名为StrCmpLogicalW的函数.您可以在自己的IComparer中使用此函数来获得与Windows资源管理器相同的排序行为.此函数将字符串的数字部分视为数字,以便9在10之前排序.
public class MyComparer : IComparer<string> {
[DllImport("shlwapi.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
static extern int StrCmpLogicalW(String x, String y);
public int Compare(string x, string y) {
return StrCmpLogicalW(x, y);
}
}
Array.Sort(unsortedNames, new MyComparer());
Run Code Online (Sandbox Code Playgroud)
因为我刚刚注意到这个问题被标记为VB ...原谅我生锈的VB!
Public Class MyComparer
Implements IComparer(Of String)
Declare Unicode Function StrCmpLogicalW Lib "shlwapi.dll" ( _
ByVal s1 As String, _
ByVal s2 As String) As Int32
Public Function Compare(Byval x as String, Byval y as String) As Integer _
Implements System.Collections.Generic.IComparer(Of String).Compare
Return StrCmpLogicalW(x, y)
End Function
End Class
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5432 次 |
| 最近记录: |