Results 1 to 1 of 1

Thread: Sorting AlphaNumeric Strings Logically as Windows File Explorer Does

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,689

    Sorting AlphaNumeric Strings Logically as Windows File Explorer Does

    C# version here.

    People often ask how to sort alphanumeric Strings logically. It is usually, although not exclusively, in the context of sorting file names. That's because Windows File Explorer does just that. As an example, Windows File Explorer would sort the following file names in the following order:
    Code:
    File1Test.txt
    File2Test.txt
    File3Test.txt
    File4Test.txt
    File5Test.txt
    File10Test.txt
    File20Test.txt
    File30Test.txt
    File40Test.txt
    File50Test.txt
    whereas a standard String sort would yield the following order:
    Code:
    File1Test.txt
    File10Test.txt
    File2Test.txt
    File20Test.txt
    File3Test.txt
    File30Test.txt
    File4Test.txt
    File40Test.txt
    File5Test.txt
    File50Test.txt
    Windows File Explorer achieves this by using the StrCmpLogicalW API function and you can do the same. The first step is to use Platform Invoke to declare this function in your .NET code so that you can invoke the unmanaged function:
    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Module NativeMethods
    4.  
    5.     <DllImport("shlwapi.dll", CharSet:=CharSet.Unicode)>
    6.     Public Function StrCmpLogicalW(x As String, y As String) As Integer
    7.     End Function
    8.  
    9. End Module
    Sorting is done in various specific ways but they all come down to comparing pairs of values and swapping them if required. You can provide the code to perform those comparisons yourself and use the API function above:
    vb.net Code:
    1. Public Class LogicalStringComparer
    2.     Implements IComparer, IComparer(Of String)
    3.  
    4.     Private Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
    5.         Return Compare(CStr(x), CStr(y))
    6.     End Function
    7.  
    8.     Public Function Compare(x As String, y As String) As Integer Implements IComparer(Of String).Compare
    9.         Return NativeMethods.StrCmpLogicalW(x, y)
    10.     End Function
    11.  
    12. End Class
    That class follows the convention of implementing both when there is a generic and non-generic version of an interface. As is done above, you should declare the non-generic method Private and have it call the generic method. That way, those who use the class directly will only have access to the generic method but code that uses a reference of the non-generic interface type will still have access to the non-generic method. More on that later. Note that you may prefer to declare the API function inside that class if that's the only place you plan to use it:
    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class LogicalStringComparer
    4.     Implements IComparer, IComparer(Of String)
    5.  
    6.     <DllImport("shlwapi.dll", CharSet:=CharSet.Unicode)>
    7.     Private Shared Function StrCmpLogicalW(x As String, y As String) As Integer
    8.     End Function
    9.  
    10.     Private Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
    11.         Return Compare(CStr(x), CStr(y))
    12.     End Function
    13.  
    14.     Public Function Compare(x As String, y As String) As Integer Implements IComparer(Of String).Compare
    15.         Return StrCmpLogicalW(x, y)
    16.     End Function
    17.  
    18. End Class
    You can then use that class to perform the comparisons wherever an IComparer or IComparer(Of String) is expected. Here is some example code that uses it to sort a String array, an ArrayList, a List(Of String) and a LINQ query producing an IEnumerable(Of String):
    vb.net Code:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Dim fileNames = {"File1Test.txt",
    5.                          "File2Test.txt",
    6.                          "File3Test.txt",
    7.                          "File4Test.txt",
    8.                          "File5Test.txt",
    9.                          "File10Test.txt",
    10.                          "File20Test.txt",
    11.                          "File30Test.txt",
    12.                          "File40Test.txt",
    13.                          "File50Test.txt"}
    14.         Dim rng As New Random
    15.  
    16.         SortArray(fileNames.OrderBy(Function(s) rng.NextDouble()).ToArray())
    17.         SortArrayList(New ArrayList(fileNames.OrderBy(Function(s) rng.NextDouble()).ToArray()))
    18.         SortList(New List(Of String)(fileNames.OrderBy(Function(s) rng.NextDouble())))
    19.         SortQuery(fileNames.OrderBy(Function(s) rng.NextDouble()).ToArray())
    20.  
    21.         Console.ReadLine()
    22.     End Sub
    23.  
    24.     Private Sub SortArray(arr As String())
    25.         Console.WriteLine("Array before:")
    26.  
    27.         For Each s In arr
    28.             Console.WriteLine(s)
    29.         Next
    30.  
    31.         Console.WriteLine()
    32.  
    33.         Array.Sort(arr, New LogicalStringComparer)
    34.  
    35.         Console.WriteLine("Array after:")
    36.  
    37.         For Each s In arr
    38.             Console.WriteLine(s)
    39.         Next
    40.  
    41.         Console.WriteLine()
    42.     End Sub
    43.  
    44.     Private Sub SortArrayList(al As ArrayList)
    45.         Console.WriteLine("ArrayList before:")
    46.  
    47.         For Each s In al
    48.             Console.WriteLine(s)
    49.         Next
    50.  
    51.         Console.WriteLine()
    52.  
    53.         al.Sort(New LogicalStringComparer)
    54.  
    55.         Console.WriteLine("ArrayList after:")
    56.  
    57.         For Each s In al
    58.             Console.WriteLine(s)
    59.         Next
    60.  
    61.         Console.WriteLine()
    62.     End Sub
    63.  
    64.     Private Sub SortList(lst As List(Of String))
    65.         Console.WriteLine("List before:")
    66.  
    67.         For Each s In lst
    68.             Console.WriteLine(s)
    69.         Next
    70.  
    71.         Console.WriteLine()
    72.  
    73.         lst.Sort(New LogicalStringComparer)
    74.  
    75.         Console.WriteLine("List after:")
    76.  
    77.         For Each s In lst
    78.             Console.WriteLine(s)
    79.         Next
    80.  
    81.         Console.WriteLine()
    82.     End Sub
    83.  
    84.     Private Sub SortQuery(arr As String())
    85.         Console.WriteLine("Query before:")
    86.  
    87.         For Each s In arr
    88.             Console.WriteLine(s)
    89.         Next
    90.  
    91.         Console.WriteLine()
    92.  
    93.         Dim qry = arr.OrderBy(Function(s) s, New LogicalStringComparer)
    94.  
    95.         Console.WriteLine("Query after:")
    96.  
    97.         For Each s In qry
    98.             Console.WriteLine(s)
    99.         Next
    100.  
    101.         Console.WriteLine()
    102.     End Sub
    103.  
    104. End Module
    I've included the ArrayList because it demonstrates that, even though the method that implements IComparer.Compare is Private, it is still accessible where a reference of type IComparer is used. The parameter of the ArrayList.Sort method is type IComparer so, inside that method, it is actually that Private overload of LogicalStringComparer.Compare that is being invoked.

    Note that the LINQ OrderBy method that accepts an IComparer(Of T) as an argument also requires you to specify a selector for the sort key of type T. In the example above, we're sorting Strings so the key is the item itself. In other cases, the sort key might be a property of the item, e.g. the Name property of a FileInfo object:
    vb.net Code:
    1. Dim folder As New DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.MyDocuments)
    2. Dim files = folder.EnumerateFiles("*.*", SearchOption.AllDirectories).
    3.                    OrderBy(Function(fi) fi.Name,
    4.                            New LogicalStringComparer).
    5.                    ToArray()
    Last edited by jmcilhinney; May 13th, 2024 at 09:19 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width