Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i have a question suppose my string contains "cdaf" and i want the out in alphabetical order like "acdf" can any one give me the code in vbscript.Thanks in advance

What I have tried:

vbs
Dim str
	 str="edfca"
	 strlength= len(str)
	 MsgBox strlength

	 For i=0 to strlength
		 For j=0 to strlength-1
          
       If j >  i Then
         TempValue = j
         i = j
         j = TempValue
      End If
Posted
Updated 6-Apr-17 0:15am
v2

1 solution

From the look of your code you are attempting a Bubble Sort[^]

The first thing to do is to get your string into a character array - you don't strictly speaking have to do this but it will make the rest of the code easier to read. Something like this:

VB
ReDim chars(strlength - 1) As String
For i = 1 To strlength
    chars(i - 1) = Mid$(str, i, 1)
Next
After that, your attempt isn't too far off the mark, except you need to be looking at and swapping the actual characters not the loop counters.
E.g.
VB
            If chars(i) > chars(j) Then
                TempValue = chars(j)
etc...

As this is obviously homework I'm not going to give you the full code - give it a go yourself with this extra information ... but I'll give you one hint:
VB
For j=0 to strlength-1
...that 0 is incorrect. Check the documentation on the link I gave you to see what it should be.
 
Share this answer
 
Comments
CPallini 6-Apr-17 8:12am    
5.
Member 13107348 6-Apr-17 9:55am    
thanks for the reply, actually i am learning vbscript thats why i asked for the code..still unable to understand
CHill60 6-Apr-17 9:58am    
Did you look at the link I provided? At the very first item on that link?
A little bit further down is a WikiBooks link. Both of these give the full code. That second item gives a full explanation of the algorithm.
Member 13107348 7-Apr-17 8:36am    
Are you talking about the Bubble Sort link , after clicking, it take us to google page, please let me know if i am wrong
CHill60 7-Apr-17 8:39am    
Yes it takes you to a Google search page. The first item returned on that page gives the full code for a bubble sort. The third link on that page takes you to a full explanation of how a bubble sort works - Algorithm Implementation/Sorting/Bubble sort - Wikibooks, open books for an open world[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900