我找到了Natural Sort Alpha-numeric like Windows Explorer in C#https://dotnetfiddle.net/kBP7EW的运行代码

Dmitry Bychenko用C#写了一些非常好的代码,它Dmitry Bychenko%工作,但不知何故在VB.NET中不工作.我不能理解在转换过程中遗漏了什么.

我刚刚将其转换为VB.NET,但收到多个错误:

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text.RegularExpressions

Public NotInheritable Class NaturalComparer
    Implements IComparer(Of String)

    Private Shared Function CompareChunks(ByVal x As String, ByVal y As String) As Integer
        If x(0) >= "0"c AndAlso x(0) <= "9"c AndAlso y(0) >= "0"c AndAlso y(0) <= "9"c Then
            Dim tx = x.TrimStart("0"c)
            Dim ty = y.TrimStart("0"c)

            Dim result = tx.Length.CompareTo(ty.Length)

            If result <> 0 Then Return result

            result = tx.CompareTo(ty)

            If result <> 0 Then Return result
        End If

        Return String.Compare(x, y)
    End Function

    Public Function Compare(ByVal x As String?, ByVal y As String?) As Integer Implements IComparer(Of String).Compare
        If ReferenceEquals(x, y) Then Return 0
        If x Is Nothing Then Return -1
        If y Is Nothing Then Return +1

        Dim itemsX = Regex.Split(x, "([0-9]+)").Where(Function(item) Not String.IsNullOrEmpty(item)).ToList()

        Dim itemsY = Regex.Split(y, "([0-9]+)").Where(Function(item) Not String.IsNullOrEmpty(item)).ToList()

        Dim i = 0

        While i < Math.Min(itemsX.Count, itemsY.Count)
            Dim result = CompareChunks(itemsX(i), itemsY(i))

            If result <> 0 Then Return result
            Threading.Interlocked.Increment(i)
        End While

        Return itemsX.Count.CompareTo(itemsY.Count)
    End Function
End Class

Public Class Program
    Public Shared Sub Main()
        Dim demo = New String() {"B01 002", "B01 0010", "01", "B01 001", "B10 001", "B01 01", "1", "B1 001", "B02 001", "A1"}

        Array.Sort(demo, New NaturalComparer())

        Console.WriteLine(String.Join(Environment.NewLine, demo))
    End Sub
End Class

错误#1:

Error BC30401
'Compare' cannot implement 'Compare' because there is no matching function on interface 'IComparer(Of String)'. ConsoleApp4 C:\Users\RamAnjana\source\repos\ConsoleApp4\Program.vb 26 N/A\

错误2:

Error BC30149
Class 'NaturalComparer' must implement 'Function Compare(x As String, y As String) As Integer' for interface 'IComparer(Of String)'.ConsoleApp4 C:\Users\RamAnjana\source\repos\ConsoleApp4\Program.vb

错误3:

Error BC33101
Type 'String' must be a value type or a type argument constrained to 'Structure' in order to be used with 'Nullable' or nullable modifier '?'. ConsoleApp4 C:\Users\RamAnjana\source\repos\ConsoleApp4\Program.vb

错误#4:

Error BC33101
Type 'String' must be a value type or a type argument constrained to 'Structure' in order to be used with 'Nullable' or nullable modifier '?'.
ConsoleApp4 C:\Users\RamAnjana\source\repos\ConsoleApp4\Program.vb

我试着用很多方法来改变它,但还是出错了.

推荐答案

String中删除?,似乎VB.NET不支持nullable reference types:

Public Function Compare(ByVal x As String, ByVal  y As String) As Integer Implements IComparer(Of String).Compare

Csharp相关问答推荐

编写DataAnnotations自定义验证器的多种方法

如何模拟耐久任务客户端在统一测试和获取错误在调度NewsListationInstanceAsync模拟设置

可为空的泛型属性

从Blob存储中提取tar.gz文件并将提取结果上载到另一个Blob存储

如何使datagridview的列具有响应性,以便不是所有列都更改其宽度

C#方法从AJAX调用接收NULL

如何在C#中实现非抛出`MinBy`?

C#-VS2022:全局使用和保存时的代码清理

为基本审计设置Audit.EntityFramework.Core

C#按名称从类获取属性值类型<;t>;,我需要反射吗?

在implementationFactory中避免循环依赖

如何实现有条件的自定义Json转换器隐藏属性

当try 测试具有协变返回类型的抽象属性时,类似功能引发System.ArgumentException

Visual Studio如何使用当前的框架?

读取测试项目中的应用程序设置

反序列化我以前使用System.Text.Json序列化的文件时出现异常

处理方法内部过滤与外部过滤

我如何为我的Blazor应用程序构建一个动态教程标注?

C#如何替换两个XML元素值?

需要帮助才能在图片框中显示C#(WinForm)中纯资源DLL中的图像