VB icon

Array SortAlpha

Email
Submitted on: 1/5/2015 10:46:00 PM
By: Andy Slowey (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: ASP (Active Server Pages)
Views: 1659
 
     Sorts arrays alphabetically, ascending or descending.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :Array SortAlpha
'**************************************
Copyright... Dont be stupid!
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Array SortAlpha
' Description:Sorts arrays alphabetically, ascending or descending.
' By: Andy Slowey (from psc cd)
'
' Inputs:Array and Sort order
'
' Returns:Array
'
' Assumes:Okay so this isnt that complicated but it is a rather old bit of code I dug up while clearing out an old hard drive. I do have a versionfloating about someplace that does multidimensional arrays which will follow when I find it!
'**************************************

<%
Function SortAlpha(ary, direction)
'###############################################################
'# USAGE: Call The function as any other, specify ASC or DESC #
'#to change the ordering direction. This is meant to#
'#be similar to SQL style syntax#
'###############################################################
StopWork=False
Do Until StopWork=True
StopWork=True
For i = 0 to UBound(ary)
If i=UBound(ary) then Exit For
If UCase(Direction) = "DESC" Then
If ary(i) < ary(i+1) Then
firstval = ary(i)
secondval = ary(i+1)
ary(i) = secondval
ary(i+1) = firstval
StopWork=False
End If
Else
If ary(i) > ary(i+1) Then
firstval = ary(i)
secondval = ary(i+1)
ary(i) = secondval
ary(i+1) = firstval
StopWork=False
End If
End If
Next
Loop
SortAlpha=ary
End Function
Function Sort(ary)
KeepChecking = TRUE
Do Until KeepChecking = FALSE
KeepChecking = FALSE
For I = 0 to UBound(ary)
If I = UBound(ary) Then Exit For
If ary(I) > ary(I+1) Then
FirstValue = ary(I)
SecondValue = ary(I+1)
ary(I) = SecondValue
ary(I+1) = FirstValue
KeepChecking = TRUE
End If
Next
Loop
Sort = ary
End Function
Function PrintArray(ary)
For i=0 to UBound(ary)
Response.Write(ary(i) & "<BR>" & vbCrLf)
Next
End Function
Dim MyArray
MyArray = Array(1,5,"shawn","says","hello",123,12,98)
PrintArray(MyArray)
Response.Write("<HR><h1>Sorted Alpha ASC</h1><br><br>")
PrintArray(SortAlpha(MyArray, "ASC"))
Response.Write("<HR><h1>Sorted Alpha DESC</h1><br><br>")
PrintArray(SortAlpha(MyArray, "DESC"))
Response.Write "<HR>"
For I = 0 to Ubound(Sort(MyArray))
Response.Write MyArray(I) & "<br>" & vbCRLF
Next
%>


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.