# 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: 3894

Sorts arrays alphabetically, ascending or descending.

Can't Copy and Paste this?
 ```'************************************** ' for :Array SortAlpha '************************************** Copyright... Dont be stupid!```
code:
Can't Copy and Paste this?
 ``` '************************************** ' 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) & "
" & vbCrLf) Next End Function Dim MyArray MyArray = Array(1,5,"shawn","says","hello",123,12,98) PrintArray(MyArray) Response.Write("

Sorted Alpha ASC

") PrintArray(SortAlpha(MyArray, "ASC")) Response.Write("

Sorted Alpha DESC

") PrintArray(SortAlpha(MyArray, "DESC")) Response.Write "
" For I = 0 to Ubound(Sort(MyArray)) Response.Write MyArray(I) & "
" & vbCRLF Next %>```

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:

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 ...)