Array sort an one dimensional array by Anders Franzen

Anders Franzen shares an easy way to sort a one dimensional array in this code sniplet.

Contributed by
Rating: 3 stars3 stars3 stars3 stars3 stars / 8
January 01, 2003
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement
BR>

</PRE><PRE><%
<
B>'**** March 2001 ****
'
AuthorAnders Franzen
'anders.franzen@banqit.com
'
'A easy way to sort a one dimensional array
'
use it a lot
'********************
</B>
Option Explicit
Function SelectionSort(TempArray, SortOrder)
Dim MaxVal, MaxIndex, i, x

<B>' 
Step through the elements in the array starting with the last element.
</
B>For UBound(TempArrayTo 0 Step -1

<B>    ' Set MaxVal to the element in the array and save the
    ' 
index of this element as MaxIndex.
</
B>    MaxVal TempArray(i)
    
MaxIndex i
    
<B>    ' Check the selected sortorder of the array
    ' "<" 
for DESC and ">" for ASC
</B>    If SortOrder "<" Then
<B>        ' Loop through the remaining elements to see if any is
        ' 
larger than MaxVal. If it is then set this element
        
' to be the new MaxVal.
</B>        For x = 0 To i
            If TempArray(x) < MaxVal Then
                MaxVal = TempArray(x)
                MaxIndex = x
            End If
        Next
    Else
        For x = 0 To i
            If TempArray(x) > MaxVal Then
                MaxVal = TempArray(x)
                MaxIndex = x
            End If
        Next
    End If

    If MaxIndex < i Then
        TempArray(MaxIndex) = TempArray(i)
        TempArray(i) = MaxVal
    End If

Next

SelectionSort = TempArray

End Function

Sub SortMyArray()
Dim TheArray, i

<B>'
Create the array      
</
B>TheArray = Array("Gambia""Sweden""France""Denmark""Estonia"
"Egypt""USA""Finland""Canada""Mexico")

<
B>'You can insert any one dimensional array here
</B>TheArray = SelectionSort(TheArray, ">") '
Use ">" for Ascending and
 
"<" for Descending
For 0 To UBound(TheArray)
    
Response.Write(TheArray(i) & "<br>")
Next

End Sub 

<B>'Use like this
</B>Call SortMyArray()
%></PRE>

blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums  
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map  
Request Media Kit
Write For Us Get Paid  
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy  
Support  


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials