Author |
Topic |
|
pdrg
Support Moderator
United Kingdom
2897 Posts |
Posted - 26 November 2002 : 07:21:14
|
Hiya - can anyone help me with this?
I want to take a word eg 'snitz' and return a string of the letters alphabetically - ie 'instz'. I can think of half a dozen ways to approach this, but really can't think what'd be most efficient algorithm.
Anyone got any suggestions of features/sample code/ways they've done this before?
Cheers |
|
Gremlin
General Help Moderator
New Zealand
7528 Posts |
Posted - 26 November 2002 : 08:14:45
|
Id just do a simple Sort, something like this probably
TestString = "Snitz"
StringLen = Len(TestString)
Dim arrResults() ' Arbitary Value
Redim arrResults(StringLen-1)
For i = 1 to len(TestString)
arrResults(i-1) = Mid(TestString,i,1)
Next
For i = ubound(arrResults) to 0 Step -1
MinMax = arrResults(0)
MinMaxSlot = 0
For j = 1 To i
If (strComp( arrResults(j), MinMax, vbTextCompare ) > 0) Then
MinMax = arrResults(j)
MinMaxSlot = j
End If
Next
If MinMaxSlot <> i Then
temp = arrResults(MinMaxSlot)
arrResults(MinMaxSlot) = arrResults(i)
arrResults(i) = temp
End If
Next
For i = 0 to StringLen -1
Response.Write arrResults(i)
Next
|
Kiwihosting.Net - The Forum Hosting Specialists
|
|
|
GauravBhabu
Advanced Member
4288 Posts |
Posted - 26 November 2002 : 08:28:03
|
strTest = "snitz"
lenStrTest = len(strTest)
strTemp = strTest
for i = 65 to 90
if instr(1, strTemp, chr(i), 1) > 0 then
strTemp = Replace(strTemp, chr(i), "", 1, 1, 1)
strSorted = strSorted & chr(i)
i = i -1
end if
next
Response.Write strSorted |
Edited by - GauravBhabu on 26 November 2002 08:30:52 |
|
|
Gremlin
General Help Moderator
New Zealand
7528 Posts |
Posted - 26 November 2002 : 08:46:40
|
Only problem with that GB is you lose the original case of the input string, and any special character in the string. Easily fixed though I guess. |
Kiwihosting.Net - The Forum Hosting Specialists
|
Edited by - Gremlin on 26 November 2002 08:48:38 |
|
|
Gremlin
General Help Moderator
New Zealand
7528 Posts |
Posted - 26 November 2002 : 08:58:02
|
I should say though that performance wise your's would be a lot quicker GB :) |
Kiwihosting.Net - The Forum Hosting Specialists
|
|
|
pdrg
Support Moderator
United Kingdom
2897 Posts |
Posted - 26 November 2002 : 09:32:33
|
Cheers guys, that was a well fast response!
I'll do a bunch of validation then probably go with GB's methodology first. If I have time I'll do a few benchmarks and post back to here, if anyone's interested.
Luckily I need this for a one-time app anyway, so speed will not be the critical factor, but it'll be interesting to see anyway! |
|
|
pdrg
Support Moderator
United Kingdom
2897 Posts |
Posted - 26 November 2002 : 09:34:03
|
D'oh,
actually it's not a one-time thingy so speed *will* be useful. I'll do some benchmarks ;-) |
|
|
Doug G
Support Moderator
USA
6493 Posts |
Posted - 26 November 2002 : 23:38:42
|
Here is a plan B approach:
<%@ Language=VBScript %>
<%
Dim sTemp, rs, i, s1
Set rs = server.CreateObject("ADODB.Recordset")
rs.Fields.Append "MyCol", 129, 1
rs.Open
'
sTemp = "This is A Test"
For i = 1 to len(sTemp)
rs.AddNew
rs.Fields("MyCol") = mid(sTemp, i, 1)
Next
'
rs.MoveFirst
rs.Sort = "MyCol ASC"
s1 = rs.GetString(,,,"")
s1 = replace(s1, chr(13), "") 'For some reason getstring puts in CR?
'
rs.Close
Set rs = nothing
%>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<P><% =s1 %></P>
</BODY>
</HTML>
|
====== Doug G ====== Computer history and help at www.dougscode.com |
|
|
GauravBhabu
Advanced Member
4288 Posts |
Posted - 27 November 2002 : 03:38:28
|
I like it, Doug G. I was thinking of using a table but your Plan B approach is better. Have you done any tests on this approach. Will this approach be better if used in place of application variables. |
|
|
Doug G
Support Moderator
USA
6493 Posts |
Posted - 27 November 2002 : 04:49:17
|
I think so. I read a MIND article a couple years ago about using disconnected ado recordsets for non-database purposes. Supposedly the sort performance is vastly better than string sorting in VBScript.
I use this technique in an application I wrote to transfer data from one computer to another, letting the app build an ado recordset then save to a file. Works pretty slick.
|
====== Doug G ====== Computer history and help at www.dougscode.com |
|
|
Gremlin
General Help Moderator
New Zealand
7528 Posts |
Posted - 27 November 2002 : 05:18:12
|
I've read some other articles that would support that, I thought about changing my members.asp to use something like that a few months ago but never got around to it. |
Kiwihosting.Net - The Forum Hosting Specialists
|
|
|
|
Topic |
|