Sorting strings with initial alphnumeric part producing alphabetic/numeric ordering?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • siobudms
    New Member
    • May 2018
    • 3

    Sorting strings with initial alphnumeric part producing alphabetic/numeric ordering?

    How do i sort the strings
    "Ch 3", "Ch 20a", "Ch 1 b", "Ch 10b"
    into the order:
    "Ch 1 b", "Ch 3", "Ch 10b", "Ch 20a"?
    I have tried using e.g.:
    Array.Sort(arra y1, StringComparer. Ordinal) but this produces the ASCII order:
    "Ch 1 b", "Ch 10b", "Ch 20a", "Ch 3".
    The strings are directory names and the initial alphanumeric part is unknown until they are retrieved with e.g. Directory.GetDi rectories(parPa th).
    Windows Explorer produces a correct alphanumeric sort oder. Does anyone know how it achieves this?
    Last edited by siobudms; May 29 '18, 09:00 AM. Reason: Additional information added
  • IronRazer
    New Member
    • Jan 2013
    • 83

    #2
    You will need to create your own Comparer class to compare alphanumeric strings. With one quick search for "Sort alphanumeric strings vb.net", the link below was the first search result at the top of the page.

    I tested the AlphanumCompara tor class that they show there and it worked fine for sorting the example strings (directory names) you show.

    dotnetperls - alphanumeric sorting

    Comment

    • siobudms
      New Member
      • May 2018
      • 3

      #3
      Hi IronRazer - Thanks for reply and I have looked at the code. I was constructing a parsing subroutine but decided to try another tack and searched with 'Visual Basic Sort Directories'. I found a simple answer dated 2009 which calls StrCmpLogicalW from the 'shlwapi.dll' library. It leaves a little niggle as it collates e.g. 'F22 2.txt' before 'F22.txt' which I would have thought was wrong. However, it solves the major issue e.g. getting chapter directories to collate correctly. I will post some code and the link to the solution. I am currently investigating other ways of including the DLL reference or the .NET equivalent to the COM object. Once again, thanks for you interest. SD (Please excuse my terminology if incorrect.)

      Comment

      • IronRazer
        New Member
        • Jan 2013
        • 83

        #4
        Yes I saw your post on the Msdn Vb.net forum (link below), I am a member there too.

        Sorting filenames using the Windows Explorer algorithm

        The class at that link I gave you sorts the names in the same order as the windows Explorer does on my testing. "F22" comes before "F22 2". It is about the same length as the code in that msdn thread but, it does not use any native win32 api functions.

        It is usually a good idea to avoid importing and using win32 methods unless you really need to do so. They can open up security risks in your app.

        Comment

        • siobudms
          New Member
          • May 2018
          • 3

          #5
          The code to use the win32 DLL is concise:
          'Declaration:
          Private myComparer = New myArraySort()
          'Calling code:
          VarrstrDirector iesLevel = Directory.GetDi rectories(parPa th)
          Array.Sort(Varr strDirectoriesL evel, myComparer)
          'Definition:
          Imports System.Runtime. InteropServices
          Public Class myArraySort
          Implements System.Collecti ons.IComparer
          Public Function Compare _ (contd.)
          ByVal parL As Object, ByVal parU As Object) _ (contd.)
          As Integer Implements IComparer.Compa re
          Dim LVResult As Integer
          Dim LVstrCEL As String
          Dim LVstrCEU As String
          LVstrCEL = String.Copy(par L)
          LVstrCEU = String.Copy(par U)
          LVcompareResult = StrCmpLogicalW( LVstrCEL, LVstrCEU)
          Return LVResult
          End Function
          <DllImport("shl wapi.dll", CharSet:=CharSe t.Unicode)>
          Public Shared Function StrCmpLogicalW( ByVal strA As String, ByVal strB As String) As Int32
          End Function
          End Class
          This is the cut down version which sorts a list box with a tag contains the paths for the corresponding files - similarly directories.

          I prefer the way the 'hands-on' code sorts the F22.txt and F22 2.txt names so will experiment with them. Do you have any idea why the Win32 DLL returns the opposite order? I believe the raw GetDirectories return the paths in date/time order? There must be a .NET implementation of the same functionality obviating the need to call the win32 DLL. I note the same DLL in in the WOW64 directory and elsewhere. Do you have links for any better documentation?

          Thanks for your time and help. SD

          Comment

          Working...