通过数组中的字符串排序?

Jer*_*Ahn 0 vb.net

每个数组字符串表示文件夹中的文本文件.我希望根据文本文件包含的内容对数组进行排序.我该怎么办?

Ry-*_*Ry- 5

您可以将自定义传递ComparisonArray.Sort,所以只需在那里读取文件:

Array.Sort(str, Function(a, b)
                    Dim aContents As String = IO.File.ReadAllText(a)
                    Dim bContents As String = IO.File.ReadAllText(b)

                    'Compare the contents and return -1 if a < b, 0 if a = b, or 1 if a > b.
                End Function)
Run Code Online (Sandbox Code Playgroud)

如果效率是一个问题,您可能希望将每个文件的内容缓存在一个Dictionary或类似的东西中.


此外,您可以使用LINQ,具体取决于您需要排序的文件中的确切内容:

Dim result = str.
    Select(Function(x) New With {.File = x, .Contents = IO.File.ReadAllText(x)}).
    OrderBy(Function(y) y.Contents)
Run Code Online (Sandbox Code Playgroud)

... 例如.