Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET中字符串路径的排序列表_Vb.net_Sorting_Path - Fatal编程技术网

VB.NET中字符串路径的排序列表

VB.NET中字符串路径的排序列表,vb.net,sorting,path,Vb.net,Sorting,Path,我想对表示路径的字符串列表进行排序。排序结果将具有层次顺序 我的意思是:对于每个目录路径,我想在第一时间列出该路径中的所有文件(字母顺序与否无关紧要)。然后,将列出每个子目录路径。对于每个子目录,我希望所有文件。。。等等 以下是一个例子: (之前) (之后) 我尝试了许多解决方案,比如带有自定义IComparable函数的Array.Sort() 你知道吗?非常感谢 编辑:这是我的IComparable方法 zipEntries.Sort(AddressOf compareZipEntryFil

我想对表示路径的字符串列表进行排序。排序结果将具有层次顺序

我的意思是:对于每个目录路径,我想在第一时间列出该路径中的所有文件(字母顺序与否无关紧要)。然后,将列出每个子目录路径。对于每个子目录,我希望所有文件。。。等等

以下是一个例子:

(之前)

(之后)

我尝试了许多解决方案,比如带有自定义IComparable函数的Array.Sort()

你知道吗?非常感谢

编辑:这是我的IComparable方法

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

将下面的插入到进行排序的类中,然后使用
zipEntries.sort(新的PathComparer)


事实证明,这很简单:

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

请发布您编写的自定义IComparable方法。我认为这是正确的方法。看起来您的函数应该可以工作-但是,它没有“实现”IComparable.CompareTo.Nope。问题是,在最后一步,在确定嵌套级别之后,应该比较目录,而不是文件名。看看我的答案。另外,您不必实现接口,还有其他重载。是的!你太棒了,你刚刚解决了我的问题!非常感谢,有件事我不明白。PathSeparator返回“;”c--我想您是用它来计算“/”,而不是“;”。所以,字符串中永远不会有“;”。哎哟。它之所以有效,是因为它有一个bug。修复错误会破坏它。伟大的我将发布固定版本。我认为
Uri.Compare
使用
String.Compare
是有原因的。。。原因是斜杠比任何字母都小。试试这个版本。有了清晰的想法,我们得到了清晰的代码。比较DirectoryName和FileName是最简单的方法。为什么?为什么我以前不这么想?!您的解决方案非常完美。这是将IComparable链接到Sort()方法的另一种方法。因为我不想添加类,所以我决定在我的项目类中创建一个函数。行为是一样的。整洁-我还没有遇到排序的比较(T)变体!
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 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
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