2010-11-12 4 views
1

J'ai créé un trieur que StrCmpLogicalW/shlwapi.dll. Malheureusement, il provoque des erreurs sur les environnements de confiance partielle. J'ai vraiment besoin d'une solution qui n'utilise pas 'shlwapi.dll' ou StrCmpLogicalW et fonctionne de la même manière.Trier les tableaux numériquement et alphabétiquement (comme Windows Explorer) sans utiliser StrCmpLogicalW ou shlwapi.dll - ASP.NET VB

S'IL VOUS PLAÎT HELPPPPP!

Voici le trieur qui provoque l'erreur.

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 

Répondre

1

Cela devrait fonctionner:

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 

Le résultat de ce qui précède le tri exemple de "0", "1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "3", "4", "5", "6", "7", "8", "9" est:

alt text

+0

Après lignes de code donnent une erreur "ToList" dans pas membre de "System.Array": Dim res1 = System.Text.RegularExpressions.Regex.Split (x, "(\ d +)", System.Text.RegularExpressions.RegexOpti ons.IgnoreCase) .ToList Dim res2 = System.Text.RegularExpressions.Regex.Split (y, "(\ d +)", System.Text.RegularExpressions.RegexOptions.IgnoreCase) .ToListe – Norman

+0

N'a pas testé. Moment ... –

+0

Maintenant, il devrait fonctionner, jetez un oeil. –