Sorting strings

M

Martin Dashper

Using Access 2002

I have a string consisting of a set of keywords separated by
semi-colons eg. Tree;Road;Rail;

I need to rearrange this string so that the individual keywords are in
alphabetical order ie. Bridge;Rail;Road;

Any ideas?

Martin Dashper
 
R

Ron Weiner

Using any version of Access 2000 and Up you can use a Split() and a Join()
with a sort in between. Here is some code that uses a bubble sort algorithm
to do the sorting. If you delimited list has only a few dozen items in it
this should be more that fast enough.

Public Function SortDelimitedString(strIn As String, strDelimiter As String)
As String
Dim a As Variant, temp As Variant, i As Integer, j As Integer
a = Split(strIn, strDelimiter)
For i = UBound(a) - 1 To 0 Step -1
For j = 0 To i
If a(j) > a(j + 1) Then
temp = a(j + 1)
a(j + 1) = a(j)
a(j) = temp
End If
Next
Next
SortDelimitedString = Join(a, strDelimiter)
End Function

If your delimited list has more than a couple hundred items then we really
should take another look at what you are doing.
 
M

Martin Dashper

Mnay thanks. My strings will never have more than ten or so items.
This works perfectly

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top