按数字和字母顺序(如Windows资源管理器)排序数组,而不使用StrCmpLogicalW或shlwapi.dll-ASP.NET VB

按数字和字母顺序(如Windows资源管理器)排序数组,而不使用StrCmpLogicalW或shlwapi.dll-ASP.NET VB,asp.net,vb.net,sorting,Asp.net,Vb.net,Sorting,我创建了一个排序器,它包含strmplogical w/shlwapi.dll。不幸的是,它会在部分信任环境中导致错误。我非常需要一个解决方案,它不使用“shlwapi.dll”或strmplogicalw,并且以同样的方式工作 请帮忙 这是导致错误的分拣机 Public Class nvSorter Implements IComparer(Of String) Declare Unicode Function StrCmpLogicalW Lib "shlwapi.dll"

我创建了一个排序器,它包含strmplogical w/shlwapi.dll。不幸的是,它会在部分信任环境中导致错误。我非常需要一个解决方案,它不使用“shlwapi.dll”或strmplogicalw,并且以同样的方式工作

请帮忙

这是导致错误的分拣机

Public Class nvSorter
    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
这应该起作用:

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Filenames() As String = New String() {"0", "1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "3", "4", "5", "6", "7", "8", "9"}
        Array.Sort(Filenames, New CustomComparer)
        MessageBox.Show(String.Join(",", Filenames))
    End Sub
End Class

Public Class CustomComparer
    Implements IComparer(Of String)

    Private Position As Integer
    Private Order As Integer = 1

    Public Sub New(Optional ByVal Ascending As Boolean = True)
        If Not Ascending Then
            Order = -1
        End If
    End Sub

    Private Shared Function EmptyText(ByVal s As String) As Boolean
        Return String.Empty.Equals(s)
    End Function

    Public Function Compare(ByVal x As String, ByVal y As String) As Integer Implements System.Collections.Generic.IComparer(Of String).Compare
        Dim res1 As New List(Of String)(System.Text.RegularExpressions.Regex.Split(x, "(\d+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
        Dim res2 As New List(Of String)(System.Text.RegularExpressions.Regex.Split(y, "(\d+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
        res1.RemoveAll(AddressOf EmptyText)
        res2.RemoveAll(AddressOf EmptyText)
        Position = 0

        For Each xstr As String In res1
            If res2.Count > Position Then
                If Not IsNumeric(xstr) AndAlso Not IsNumeric(res2(Position)) Then
                    Dim intresult As Integer = String.Compare(xstr, res2(Position), True)
                    If intresult <> 0 Then
                        Return intresult * Order
                    Else
                        Position += 1
                    End If
                ElseIf IsNumeric(xstr) And Not IsNumeric(res2(Position)) Then
                    Return -1 * Order
                ElseIf Not IsNumeric(xstr) And IsNumeric(res2(Position)) Then
                    Return 1 * Order
                ElseIf IsNumeric(xstr) And IsNumeric(res2(Position)) Then
                    Dim res As Integer = Decimal.Compare(Decimal.Parse(xstr), Decimal.Parse(res2(Position)))
                    If res = 0 Then
                        Position += 1
                    Else
                        Return res * Order
                    End If
                End If
            Else
                Return -1 * Order
            End If
        Next

        Return 1 * Order
    End Function
End Class
公共类表单1
私有子按钮1\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击
Dim Filenames()作为字符串=新字符串(){“0”、“1”、“10”、“11”、“12”、“13”、“14”、“15”、“16”、“17”、“18”、“19”、“2”、“20”、“3”、“4”、“5”、“6”、“7”、“8”、“9”}
Array.Sort(文件名、新CustomComparer)
MessageBox.Show(String.Join(“,”,文件名))
端接头
末级
公共类客户比较器
实现(字符串的)IComparer
作为整数的私有位置
私有顺序为整数=1
Public Sub New(可选的ByVal升序为Boolean=True)
如果不是上升的话
顺序=-1
如果结束
端接头
私有共享函数EmptyText(ByVal s作为字符串)作为布尔值
返回字符串.Empty.Equals(s)
端函数
公共函数Compare(ByVal x作为字符串,ByVal y作为字符串)作为整数实现System.Collections.Generic.IComparer(字符串的)。Compare
Dim res1作为新列表(字符串)(System.Text.RegularExpressions.Regex.Split(x,“(\d+”),System.Text.RegularExpressions.RegexOptions.IgnoreCase))
Dim res2作为新列表(字符串)(System.Text.RegularExpressions.Regex.Split(y,“(\d+”),System.Text.RegularExpressions.RegexOptions.IgnoreCase))
res1.RemoveAll(EmptyText的地址)
res2.RemoveAll(EmptyText的地址)
位置=0
对于res1中的每个xstr As字符串
如果res2.Count>位置,则
如果不是数字(xstr)也不是数字(res2(位置)),则
Dim intresult为整数=字符串。比较(xstr,res2(位置),True)
如果结果为0,则
返回结果*订单
其他的
位置+=1
如果结束
ElseIf为数字(xstr)而非数字(res2(位置)),则
退货-1*订单
如果不是IsNumeric(xstr)和IsNumeric(res2(位置)),则
返回1*订单
ElseIf IsNumeric(xstr)和IsNumeric(res2(位置))然后
Dim res As Integer=Decimal.Compare(Decimal.Parse(xstr)、Decimal.Parse(res2(Position)))
如果res=0,则
位置+=1
其他的
返回res*订单
如果结束
如果结束
其他的
退货-1*订单
如果结束
下一个
返回1*订单
端函数
末级
上述排序示例的
“0”、“1”、“10”、“11”、“12”、“13”、“14”、“15”、“16”、“17”、“18”、“19”、“2”、“20”、“3”、“4”、“5”、“6”、“7”、“8”、“9”的结果为:

这应该可以:

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Filenames() As String = New String() {"0", "1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "3", "4", "5", "6", "7", "8", "9"}
        Array.Sort(Filenames, New CustomComparer)
        MessageBox.Show(String.Join(",", Filenames))
    End Sub
End Class

Public Class CustomComparer
    Implements IComparer(Of String)

    Private Position As Integer
    Private Order As Integer = 1

    Public Sub New(Optional ByVal Ascending As Boolean = True)
        If Not Ascending Then
            Order = -1
        End If
    End Sub

    Private Shared Function EmptyText(ByVal s As String) As Boolean
        Return String.Empty.Equals(s)
    End Function

    Public Function Compare(ByVal x As String, ByVal y As String) As Integer Implements System.Collections.Generic.IComparer(Of String).Compare
        Dim res1 As New List(Of String)(System.Text.RegularExpressions.Regex.Split(x, "(\d+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
        Dim res2 As New List(Of String)(System.Text.RegularExpressions.Regex.Split(y, "(\d+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
        res1.RemoveAll(AddressOf EmptyText)
        res2.RemoveAll(AddressOf EmptyText)
        Position = 0

        For Each xstr As String In res1
            If res2.Count > Position Then
                If Not IsNumeric(xstr) AndAlso Not IsNumeric(res2(Position)) Then
                    Dim intresult As Integer = String.Compare(xstr, res2(Position), True)
                    If intresult <> 0 Then
                        Return intresult * Order
                    Else
                        Position += 1
                    End If
                ElseIf IsNumeric(xstr) And Not IsNumeric(res2(Position)) Then
                    Return -1 * Order
                ElseIf Not IsNumeric(xstr) And IsNumeric(res2(Position)) Then
                    Return 1 * Order
                ElseIf IsNumeric(xstr) And IsNumeric(res2(Position)) Then
                    Dim res As Integer = Decimal.Compare(Decimal.Parse(xstr), Decimal.Parse(res2(Position)))
                    If res = 0 Then
                        Position += 1
                    Else
                        Return res * Order
                    End If
                End If
            Else
                Return -1 * Order
            End If
        Next

        Return 1 * Order
    End Function
End Class
公共类表单1
私有子按钮1\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击
Dim Filenames()作为字符串=新字符串(){“0”、“1”、“10”、“11”、“12”、“13”、“14”、“15”、“16”、“17”、“18”、“19”、“2”、“20”、“3”、“4”、“5”、“6”、“7”、“8”、“9”}
Array.Sort(文件名、新CustomComparer)
MessageBox.Show(String.Join(“,”,文件名))
端接头
末级
公共类客户比较器
实现(字符串的)IComparer
作为整数的私有位置
私有顺序为整数=1
Public Sub New(可选的ByVal升序为Boolean=True)
如果不是上升的话
顺序=-1
如果结束
端接头
私有共享函数EmptyText(ByVal s作为字符串)作为布尔值
返回字符串.Empty.Equals(s)
端函数
公共函数Compare(ByVal x作为字符串,ByVal y作为字符串)作为整数实现System.Collections.Generic.IComparer(字符串的)。Compare
Dim res1作为新列表(字符串)(System.Text.RegularExpressions.Regex.Split(x,“(\d+”),System.Text.RegularExpressions.RegexOptions.IgnoreCase))
Dim res2作为新列表(字符串)(System.Text.RegularExpressions.Regex.Split(y,“(\d+”),System.Text.RegularExpressions.RegexOptions.IgnoreCase))
res1.RemoveAll(EmptyText的地址)
res2.RemoveAll(EmptyText的地址)
位置=0
对于res1中的每个xstr As字符串
如果res2.Count>位置,则
如果不是数字(xstr)也不是数字(res2(位置)),则
Dim intresult为整数=字符串。比较(xstr,res2(位置),True)
如果结果为0,则
返回结果*订单
其他的
位置+=1
如果结束
ElseIf为数字(xstr)而非数字(res2(位置)),则
退货-1*订单
如果不是IsNumeric(xstr)和IsNumeric(res2(位置)),则
返回1*订单
ElseIf IsNumeric(xstr)和IsNumeric(res2(位置))然后
Dim res As Integer=Decimal.Compare(Decimal.Parse(xstr)、Decimal.Parse(res2(Position)))
如果res=0,则
位置+=1
其他的
返回res*订单
如果结束
如果结束
其他的
退货-1*订单
如果结束
下一个
返回1*订单
端函数
末级
上述排序示例的
“0”、“1”、“10”、“11”、“12”、“13”、“14”、“15”、“16”、“17”、“18”、“19”、“2”、“20”、“3”、“4”、“5”、“6”、“7”、“8”、“9”的结果为:

Fol