English 中文(简体)
VB 铺设通道清单。 NET
原标题:Sorting list of string paths in VB.NET

我愿列出一条代表道路的座标。 这种结果确实具有等级顺序。

我指的是:就每个目录而言,我想首次在这条道路上列出所有档案(按字母顺序排列或无顺序排列)。 然后,将列出每一次子路线。 对于每个次主题,我想所有文件......等等。

例如:

(贝鲁特)

emule/changelog.txt
emule/config/
emule/config/adresses.dat
emule/config/nodes.dat
emule/config/webservices.dat
emule/eMule.tmpl
emule/eMule_Chicane.tmpl
emule/license.txt
emule/license-GER.txt
emule/readme.txt
emule/Skin/
emule/Skin/Daan-V2-8.eMuleSkin.ini
emule/Skin/DaanV2-8/
emule/Skin/DaanV2-8/back.ICO
emule/Skin/DaanV2-8/WebServer.ico
emule/Template.eMuleSkin.ini
emule/webserver/
emule/webserver/add_server.gif
emule/webserver/arrow_down.gif
emule/webserver/arrow_right.gif
emule/webserver/yellow.gif
emule/emule.exe

(After)

emule/changelog.txt
emule/emule.exe
emule/eMule.tmpl
emule/eMule_Chicane.tmpl
emule/license.txt
emule/license-GER.txt
emule/readme.txt
emule/Template.eMuleSkin.ini
emule/config/
emule/config/adresses.dat
emule/config/nodes.dat
emule/config/webservices.dat
emule/Skin/
emule/Skin/Daan-V2-8.eMuleSkin.ini
emule/Skin/DaanV2-8/
emule/Skin/DaanV2-8/back.ICO
emule/Skin/DaanV2-8/WebServer.ico
emule/webserver/
emule/webserver/add_server.gif
emule/webserver/arrow_down.gif
emule/webserver/arrow_right.gif
emule/webserver/yellow.gif

我尝试了许多解决办法,如Array.Sort()的习俗可比较功能。

你们是否有任何想法? 非常感谢。

EDIT:这里是我的可比较方法

zipEntries.Sort(AddressOf compareZipEntryFilenames)

Private Function compareZipEntryFilenames(ByVal x As Object, ByVal y As Object) As Integer
        Dim one As String = CType(x, ZipEntry).FileName
        Dim two As String = CType(y, ZipEntry).FileName

        If Path.GetDirectoryName(one) = Path.GetDirectoryName(two) Then
            Return String.Compare(one, two)
        Else
           Select Regex.Matches(one, "/").Count.CompareTo(Regex.Matches(two, "/").Count)
                Case -1  one has less / than two; so one then two
                    Return -1
                Case 1  one has more / than two; so two then one
                    Return 1
                Case Else   = 0, same number of /; so alphabetical sorting
                    Return String.Compare(one, two)
            End Select
        End If
    End Function
最佳回答

选择简单如下:

Private Function compareZipEntryFilenames(ByVal x As ZipEntry, ByVal y As ZipEntry) As Integer

    Dim res As Integer = String.Compare(Path.GetDirectoryName(x.FileName), Path.GetDirectoryName(y.FileName))

    If res = 0 Then
        Return String.Compare(x.FileName, y.FileName)
    Else
        Return res
    End If

End Function
问题回答

将下列内容列入发生此类情况的类别,然后使用zipEntries.Sort(new PathComparer)

private class PathComparer
        implements IComparer

     Public Function compareZipEntryFilenames(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
        Dim one As String = CType(x, ZipEntry).FileName
        Dim two As String = CType(y, ZipEntry).FileName

        If Path.GetDirectoryName(one) = Path.GetDirectoryName(two) Then
            Return String.Compare(one, two)
        Else
           Select Regex.Matches(one, "/").Count.CompareTo(Regex.Matches(two, "/").Count)
                Case -1  one has less / than two; so one then two
                    Return -1
                Case 1  one has more / than two; so two then one
                    Return 1
                Case Else   = 0, same number of /; so alphabetical sorting
                    Return String.Compare(one, two)
            End Select
        End If
      End Function      
    end class




相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签