Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I? : Alphabeticise a string 3

Status
Not open for further replies.

thefarg

Programmer
Jan 25, 2012
94
NZ
thread707-834151

The above thread has a great bit of code, only it puts the string into reverse alphabetical order. Anyone know how to modify the code to produce normal alphabetical order?
Any help much appreciated,

Mike
 

You will get a lot better responce if you would state: this is what I have now, and this is what I would like to have. And give some example of the data and the code you have.

Have fun.

---- Andy
 


hi thefarg (Programmer),

Please show what you have tried and where you have problems.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
The code I have is taken from the article in my first post, by the member PHV. It is :

x = "QWERTYASD"
For i = 1 To Len(x) - 1
c = Mid(x, i, 1)
For j = i + 1 To Len(x)
d = Mid(x, j, 1)
If d > c Then
x = Left(x, i - 1) & d & Mid(x, i + 1, j - i - 1) & c & Mid(x, j + 1)
i = 0: Exit For
End If
Next j
Next i
msgbox x

it Produces a string that has been sorted in reverse Alphabetical order. How do I modify this to sort in forward Alphabetical order?
 



Have you tried ANYTHING?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Yes, replacing the signs on several numbers, changing Left to Right. I am unsure whether I will have to rewrite it or if I only have to change several parameters. Any ideas?
 


EXACTLY WHAT did you try?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
As I said in the last post. It is a fairly simple piece of code for some of the people here and I was just hoping someone could point me in the right direction.
 

The easy way out would be:
[tt]
MsgBox StrReverse(x)
[/tt]
but that's not what you are after, I would guess....

Have fun.

---- Andy
 

Well I would do the same thing that YOU, or any other programmer, would have to do to figure this out.

What have you done? Let's see some sweat!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I was going to use it but it seems like an ugly kludge to add to a nice bit of code. Any way to PM the original author, PHV?
 

With a LITTLE bit of effort, a PROGRAMMER, like YOU, ought to be able to figure this out.

It took me about a minute and 9 seconds.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Sorry mate I'm only asking for help because I'm having trouble following the loop. I'll wait a day or so and try another forum if I have no luck here.
 

Another way to do it would be to put a break point at the start and step thru the code and SEE what is going on. Modify code where needed.

Have fun.

---- Andy
 
Thanks Andrzejek, obviously I am rather an amateur at this.
 


farg,

Andy or PHV or I or many others could serve this up to you, but part of the beauty of Tek-Tips is LEARNING and part of learning is doing some work.

Andy gave you a tip. If you do some work here, you'll get some help.

But if you call yourself a PROGRAMMER, then YOU ought to have the tools to do the analysis or ask the proper questions.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks guys, as obvious as it may have been to you I still had trouble tracking the loop. Here is the change i needed to make in case it helps anyone.

x = "QWERTYASD"
For i = 1 To Len(x) - 1
c = Mid(x, i, 1)
For j = i + 1 To Len(x)
d = Mid(x, j, 1)
'If d > c Then
If d < c Then
x = Left(x, i - 1) & d & Mid(x, i + 1, j - i - 1) & c & Mid(x, j + 1)
i = 0: Exit For
End If
Next j
Next i
msgbox x

Cheers
 


There you go! ==> *

Now what you could do is make a function that sorts EITHER way...
Code:
Function SortString(x As String, Optional bASC As Boolean = True) As String
    Dim i, j, c, d
    For i = 1 To Len(x) - 1
      c = Mid(x, i, 1)
      For j = i + 1 To Len(x)
        d = Mid(x, j, 1)
        
        If bASC Then
            If d < c Then
              x = Left(x, i - 1) & d & Mid(x, i + 1, j - i - 1) & c & Mid(x, j + 1)
              i = 0: Exit For
            End If
        Else
            If d > c Then
              x = Left(x, i - 1) & d & Mid(x, i + 1, j - i - 1) & c & Mid(x, j + 1)
              i = 0: Exit For
            End If
        End If
      Next j
    Next i
    SortString = x
End Function

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Skip, I'd replace this:
If bASC Then
If d < c Then
x = Left(x, i - 1) & d & Mid(x, i + 1, j - i - 1) & c & Mid(x, j + 1)
i = 0: Exit For
End If
Else
If d > c Then
x = Left(x, i - 1) & d & Mid(x, i + 1, j - i - 1) & c & Mid(x, j + 1)
i = 0: Exit For
End If
End If
with this:
If (bASC And d < c) Or (bASC = False And d > c) Then
x = Left(x, i - 1) & d & Mid(x, i + 1, j - i - 1) & c & Mid(x, j + 1)
i = 0: Exit For
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 


Thanx PHV.

I had looked at that yesterday and wondered how I could further distil, but I did not get to your succinct statement.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top