1

I created a sorter that StrCmpLogicalW / shlwapi.dll. unfortunately it causes errors on partial trust environments. I am in a really bad need of a solution that does not use 'shlwapi.dll' or StrCmpLogicalW and works in the same fashion.

Here's the sorter that causes the error.

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
Steven
  • 166,672
  • 24
  • 332
  • 435
Norman
  • 705
  • 1
  • 10
  • 24

1 Answers1

1

This should work:

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

The result of the above sorting example of "0", "1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "3", "4", "5", "6", "7", "8", "9" is:

alt text

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Following lines of code give an error "ToList" in not a member of "System.Array": Dim res1 = System.Text.RegularExpressions.Regex.Split(x, "(\d+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase).ToList Dim res2 = System.Text.RegularExpressions.Regex.Split(y, "(\d+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase).ToList – Norman Nov 12 '10 at 14:51
  • Tim .. thank you for all your help. There are no errors ... but its not sorting at all , I think. – Norman Nov 12 '10 at 15:37
  • You have a WinFowm `Form1` with a Button `Button1` on it? It is sorting, but I'm not sure if its exactly what you wanted. – Tim Schmelter Nov 12 '10 at 15:41
  • Array without sorting of any kind : ["0","1","10","11","12","13","14","15","16","17","18","19","2","20","3","4","5","6","7","8","9"] with new sorting applied = ["3","0","20","4","2","8","5","7","19","6","18","11","17","10","12","1","16","13","15","9","14"]; – Norman Nov 12 '10 at 15:50
  • with new sorting applied : ["3","0","20","4","2","8","5","7","19","6","18","11","17","10","12","1","16","13","15","9","14"]; – Norman Nov 12 '10 at 15:52
  • I think its not doing what was intended. :) – Norman Nov 12 '10 at 15:53
  • The same array with the old StrCmpLogicalW sorting applied: ["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]; – Norman Nov 12 '10 at 15:55
  • Have a look at my updated answer, you will see that this is exactly the result of "my" CustomComparer ;-) How do you have implemented the Comparer? – Tim Schmelter Nov 12 '10 at 15:56
  • :) silly mistake .. sorry ... Workd beautifully now :) Thank you super much ! you saved me ! – Norman Nov 12 '10 at 16:10