473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

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(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.

4 2086
IronRazer
83 64KB
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
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
83 64KB
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
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

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

Similar topics

2
by: xyz | last post by:
I am trying to sort a string array. I am trying to use a collator class, but I get errors. Do I need to an "import" statement or something? I would appreciate your suggestions. ...
3
by: amber | last post by:
Hello, Is there a simple way to sort a listbox that contains strings that are numbers? My listbox is populated from a database field that has a datatype of string, but typically contains numbers....
1
by: Neil Grantham | last post by:
Hi I have a Report based on a query, whose code looks like this: SELECT Nursery.ChildID, Nursery., Nursery., Nursery., Nursery.ChildDOB, GetIntakeTerm() AS IntakeTerm FROM Nursery GROUP BY...
3
by: Daniel Weinand | last post by:
hello ng, i have a problem and a imho an insufficient method of solution. strings should be sorted by specific text pattern and dispayed in groups. the strings are stored in a db and have the...
1
by: | last post by:
I'm querying Index Server to return search results, both regular properties and some custom properties I've created. Index Server has this preference for thinking about information as strings...
8
by: DierkErdmann | last post by:
Hi ! I know that this topic has been discussed in the past, but I could not find a working solution for my problem: sorting (lists of) strings containing special characters like "ä", "ü",......
5
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
4
by: Holger | last post by:
I tried to do this elegantly, but did not come up with a good solution Sort strings like foo1bar2 foo10bar10 foo2bar3 foo10bar2 So that they come out: foo1bar2
9
by: Timothy Chan | last post by:
I am taking C program course and i got some problem about sorting strings, hope someone could help me to solve the problem... The is the output which suppose to be: ------------------------- car...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.