473,379 Members | 1,386 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,379 software developers and data experts.

Sorting a string

344 Expert 100+
I need to sort a string, actually several thousand strings, and my head aches and I can't remember if there is a built in function to do this. (Access 2003)

I have a string, say, "BDCA" and I want to return "ABCD"

Is there a built in function for this, or do I have to remember my school lessons of ages ago and write some bubble sort function to do this?
Jul 25 '07 #1
15 6924
JKing
1,206 Expert 1GB
There is a built-in sort function which through a little searching I just found myself. It's actually a hidden class. The class is Called Wizhook. You can find it in the object browser by turning on show hidden members. There's quite a few neat members to the class. The one you are looking for is SortStringArray.

Here's a look at it:
Expand|Select|Wrap|Line Numbers
  1. Dim str As String
  2. Dim charArray(4) As String
  3. Dim intx As Integer
  4. charArray(0) = "b"
  5. charArray(1) = "D"
  6. charArray(2) = "E"
  7. charArray(3) = "c"
  8. charArray(4) = "a"
  9.  
  10. WizHook.SortStringArray charArray
  11.  
  12. str = Join(charArray, "")
  13.  
  14. MsgBox str
  15.  
I looked for a method for splitting strings into character arrays and I havent come across one yet. Split unfrotunately doesn't work if there isnt a delimiter. Specifying "" just returns the string itself and not omitting a delimiter defaults it to " ". Unless there is some wildcard that can be use that would delimit by each character I really dont know.
Jul 25 '07 #2
Lysander
344 Expert 100+
There is a built-in sort function which through a little searching I just found myself. It's actually a hidden class. The class is Called Wizhook. You can find it in the object browser by turning on show hidden members. There's quite a few neat members to the class. The one you are looking for is SortStringArray.

Here's a look at it:
Expand|Select|Wrap|Line Numbers
  1. Dim str As String
  2. Dim charArray(4) As String
  3. Dim intx As Integer
  4. charArray(0) = "b"
  5. charArray(1) = "D"
  6. charArray(2) = "E"
  7. charArray(3) = "c"
  8. charArray(4) = "a"
  9.  
  10. WizHook.SortStringArray charArray
  11.  
  12. str = Join(charArray, "")
  13.  
  14. MsgBox str
  15.  
I looked for a method for splitting strings into character arrays and I havent come across one yet. Split unfrotunately doesn't work if there isnt a delimiter. Specifying "" just returns the string itself and not omitting a delimiter defaults it to " ". Unless there is some wildcard that can be use that would delimit by each character I really dont know.
Thanks, just what I need. I can split the string by using len(str) and mid(str,x,y).
Jul 26 '07 #3
JKing
1,206 Expert 1GB
Great! I was just hoping there was a string function that will send all of a string's characters to an array instead of having to loop through it yourself. There was a handy function in C# to do this. But of course no two languages are the same.
Jul 26 '07 #4
Lysander
344 Expert 100+
Great! I was just hoping there was a string function that will send all of a string's characters to an array instead of having to loop through it yourself. There was a handy function in C# to do this. But of course no two languages are the same.
Unfortunatly, when I used wizhook.sortstringarray it sat there for a bit, and then crashed access, repeatadly no matter how simple I made the function.

I did find a simple QuickSort routine on another site. Unfortunatly the copyright code in the routine says I can use it freely, but can't post it onto any other web site, so I can't list it here.

Thanks for you help, I'll investigate wizhook in detail when I get some free time.
Jul 26 '07 #5
JKing
1,206 Expert 1GB
Hey again, it seems WizHook requires an activation key. In my research I came across the Key property being used just before the sort function and thought the Key property was irreleveant and just removed it. Especially after I removed the line and it kept working I thought it safe to assume the line did nothing relevant. Though I had executed the code once with the key property which would have activated the class.

So if you're still interested and or curious about WizHook add in this line above the sort one.

Expand|Select|Wrap|Line Numbers
  1. WizHook.Key = 51488399
  2. WizHook.SortStringArray charArray
  3.  
Also here is a link with a PDF that documents the members of WizHook.

WizHook Documentation
Jul 26 '07 #6
JKing
1,206 Expert 1GB
Actually it seems that the Key is not required for this particular method. Though I've just been reading through this help file and there is a note for the method declaring this:

This will crash access if you pass it an array with uninitialized elements.
Jul 26 '07 #7
Snoday
2
I ran across this old post when looking for Wizhook use. I noticed you asked for an easy way to sort a non delimited string, and developed the below function.
Expand|Select|Wrap|Line Numbers
  1. Function fNonDelimitedStrSorter(strInput As String) As Variant
  2. Dim iloop As Integer
  3. Dim istrLength As Integer
  4. Dim charArray() As String
  5. istrLength = Len(strInput)
  6. If istrLength > 0 Then ' String Passed Continue
  7. ReDim charArray(istrLength - 1) '-1 for shift to zero based list
  8.     For iloop = LBound(charArray()) To UBound(charArray())
  9.             charArray(iloop) = Mid(strInput, iloop + 1, 1)
  10.     Next
  11.     With WizHook
  12.         .Key = 51488399
  13.         .SortStringArray charArray
  14.         '.WizMsgBox "Sorted Array" & vbCrLf & Join(charArray, vbCrLf), "The Wizhook Caption", vbOK, -1, ""
  15.     End With
  16.     fNonDelimitedStrSorter = Join(charArray, ", ")
  17. Else
  18.     fNonDelimitedStrSorter = "No Data Passed to function - "
  19. End If
  20.     Erase charArray()
  21. End Function
  22.  
Dec 23 '11 #8
Killer42
8,435 Expert 8TB
Hold on, there is a VB function to split a string into an array, isn't there? Though I think that'd be a byte array.
Dec 24 '11 #9
NeoPa
32,556 Expert Mod 16PB
There's a function (Split()) that allows you to split a string into a Variant array of string type Variants based on a separator character if that's any help.
Dec 24 '11 #10
Killer42
8,435 Expert 8TB
There's a function (Split()) that allows you to split a string into a Variant array of string type Variants based on a separator character if that's any help.
Hm, probably not a lot of help in this case, since it requires a delimiter.

After a bit of research, I've come to the conclusion that I was imagining the string-to-char-array function. It doesn't exist in these older versions of VB (though it does in the .Net framework).

Looks as though you'd need to build your own. Which is perfectly simple, of course, using a loop and the Mid() function. And would be a handy function to keep in your toolkit.
Dec 27 '11 #11
NeoPa
32,556 Expert Mod 16PB
I wasn't sure what you were after TBF. Split() can be used without the separator parameter, but it still uses a default of comma (,) in that case. probably no use, but it's there in case it might be ;-)
Dec 27 '11 #12
Killer42
8,435 Expert 8TB
Ok, I got a bit carried away here. Couldn't find any old sort routines, so I wrote a new one. Also a couple of functions to split a string into arrays of Byte and String format (the latter of which is used by the sort function).

Here's the code. Note that it's just something I banged out, and not extensively tested for either correct results or performance. Feel free to improve it. From a quick test it does appear to work, but use at your own risk. :-)

Expand|Select|Wrap|Line Numbers
  1. Public Function SortedString(ByVal Src As String, Optional Descending As Boolean = False, Optional TrimSpaces As Boolean = False) As String
  2.   ' Take a string and sort the characters within it. Eg. "BCDA" --> "ABCD"
  3.  
  4.   Dim c() As String, TempChar As String
  5.   Dim LowestSwap As Long, HighestSwap As Long
  6.   Dim DoSwap As Boolean, AnySwapped As Boolean
  7.   Dim I As Long
  8.   Dim S As Long, E As Long
  9.  
  10.   If TrimSpaces Then Src = Trim$(Src)
  11.   If Src = "" Then
  12.     Exit Function
  13.   ElseIf Len(Src) = 1 Then
  14.     SortedString = Src
  15.     Exit Function
  16.   End If
  17.   c = String2CharArray(Src)
  18.   S = 1: E = UBound(c) - 1
  19.   Do
  20.     AnySwapped = False
  21.     HighestSwap = E
  22.     For I = S To E
  23.       If (Descending = True And c(I) < c(I + 1)) Or _
  24.          (Descending = False And c(I) > c(I + 1)) Then
  25.         ' Swap this pair of characters.
  26.         TempChar = c(I)
  27.         c(I) = c(I + 1)
  28.         c(I + 1) = TempChar
  29.         HighestSwap = I
  30.         AnySwapped = True
  31.       End If
  32.     Next
  33.     If Not AnySwapped Then Exit Do
  34.     E = HighestSwap
  35.     AnySwapped = False
  36.     For I = E To S Step -1
  37.       If (Descending = True And c(I) < c(I + 1)) Or _
  38.          (Descending = False And c(I) > c(I + 1)) Then
  39.         ' Swap this pair of characters.
  40.         TempChar = c(I)
  41.         c(I) = c(I + 1)
  42.         c(I + 1) = TempChar
  43.         LowestSwap = I
  44.         AnySwapped = True
  45.       End If
  46.     Next
  47.     S = LowestSwap
  48.   Loop While AnySwapped
  49.   SortedString = Join(c, "")
  50. End Function
  51.  
  52.  
  53.  
  54. Public Function String2ByteArray(ByVal Src As String, Optional LtrimFirst As Boolean = False, Optional RtrimFirst As Boolean = False) As Byte()
  55.   ' Split a string into a Byte array.
  56.   Dim c() As Byte
  57.   Static L As Long, I As Long
  58.   If LtrimFirst Then Src = LTrim$(Src)
  59.   If RtrimFirst Then Src = RTrim$(Src)
  60.   If Src = "" Then Exit Function
  61.   L = Len(Src)
  62.   ReDim c(1 To L)
  63.   For I = 1 To L
  64.     c(I) = Asc(Mid$(Src, I, 1))
  65.   Next
  66.   String2ByteArray = c
  67. End Function
  68.  
  69.  
  70. Public Function String2CharArray(ByVal Src As String, Optional LtrimFirst As Boolean = False, Optional RtrimFirst As Boolean = False) As String()
  71.   ' Split characters in a string into a string array.
  72.   Dim c() As String
  73.   Static L As Long, I As Long
  74.   If LtrimFirst Then Src = LTrim$(Src)
  75.   If RtrimFirst Then Src = RTrim$(Src)
  76.   If Src = "" Then Exit Function
  77.   L = Len(Src)
  78.   ReDim c(1 To L)
  79.   For I = 1 To L
  80.     c(I) = Mid$(Src, I, 1)
  81.   Next
  82.   String2CharArray = c
  83. End Function
  84.  
A couple of notes.
  • This code is entirely self-contained (apart from the fact that the SortedString function is dependent on the String2CharArray function). So to try it out you can just start a new project, paste into a code module, then go to the immediate window and try for example...
    ? sortedstring("KHD FKLD F ")
  • I've included a few parameters to allow control over how the functions operate, such as trimming off extra spaces and reversing the sort sequence.
Dec 28 '11 #13
Killer42
8,435 Expert 8TB
Sorry, forgot this was Access/VBA. I was writing for VB6. Hopefully it'll still work the same (fingers crossed), but one or two references such as "create a new project" may need to be taken with a grain of salt.
Dec 28 '11 #14
NeoPa
32,556 Expert Mod 16PB
Seems like good VBA to me Killer. I checked, but not carefully, and all seems VBA consistent.
Dec 28 '11 #15
Killer42
8,435 Expert 8TB
Thanks. I do tend to get tripped up by little differences when working with VBA, but that's usually more to do with references to form fields and the like.
Dec 28 '11 #16

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

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
12
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
8
by: nidhog | last post by:
Hello guys, I made a script that extracts strings from a binary file. It works. My next problem is sorting those strings. Output is like: ---- snip ---- 200501221530
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
1
by: Ahmed Yasser | last post by:
Hi all, i have a problem with the datagridview sorting, the problem is a bit complicated so i hope i can describe in the following steps: 1. i have a datagridview with two columns...
1
by: castron | last post by:
Hello All, I have a grid view that allows sorting, paging, editing, etc. Under On Load event, if I check: if(!IsPostBack){ DisplayData(); }, the Edit portion works fine. However, the Sorting...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.