排序文件夹名称数组,如Windows资源管理器(数字和字母) - VB.NET

时间:2010-06-23 07:08:15

标签: c# .net 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

我想拥有的(以及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

我用Google搜索并发现需要编写一个使用IComparable对元素进行排序的类。作为超级新手......我真的不知道怎么做。我看过的大多数例子都有多维数组和键:S ...

如果排序可以应用于文件名数组(而不是foldernames)或包含文件夹和文件的数组,那么

会更好......在这种情况下,排序的文件夹显示在顶部,排序的文件显示在下面。这甚至可能吗?

任何帮助都会受到极大关注......:谢谢。

2 个答案:

答案 0 :(得分: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());

因为我刚注意到这个问题被标记为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

答案 1 :(得分:1)

Array.Sort也有一个IComparer参数,如果您不喜欢默认值,可以覆盖排序行为。请参阅Array.Sort Method (T[], IComparer)如何操作

相关问题