423,491 Members | 2,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,491 IT Pros & Developers. It's quick & easy.

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

P: 3
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(array1, 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.GetDirectories(parPath).
Windows Explorer produces a correct alphanumeric sort oder. Does anyone know how it achieves this?
May 28 '18 #1

✓ answered by IronRazer

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.

Share this Question
Share on Google+
4 Replies


IronRazer
P: 81
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 AlphanumComparator class that they show there and it worked fine for sorting the example strings (directory names) you show.

dotnetperls - alphanumeric sorting
Jun 1 '18 #2

P: 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.)
Jun 1 '18 #3

IronRazer
P: 81
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.
Jun 1 '18 #4

P: 3
The code to use the win32 DLL is concise:
'Declaration:
Private myComparer = New myArraySort()
'Calling code:
VarrstrDirectoriesLevel = Directory.GetDirectories(parPath)
Array.Sort(VarrstrDirectoriesLevel, myComparer)
'Definition:
Imports System.Runtime.InteropServices
Public Class myArraySort
Implements System.Collections.IComparer
Public Function Compare _ (contd.)
ByVal parL As Object, ByVal parU As Object) _ (contd.)
As Integer Implements IComparer.Compare
Dim LVResult As Integer
Dim LVstrCEL As String
Dim LVstrCEU As String
LVstrCEL = String.Copy(parL)
LVstrCEU = String.Copy(parU)
LVcompareResult = StrCmpLogicalW(LVstrCEL, LVstrCEU)
Return LVResult
End Function
<DllImport("shlwapi.dll", CharSet:=CharSet.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
Jun 1 '18 #5

Post your reply

Sign in to post your reply or Sign up for a free account.