VB icon

Sort arrays

Email
Submitted on: 1/5/2015 12:15:00 PM
By: Rogier Doekes (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: ASP (Active Server Pages)
Views: 1944
 
     Vbscript function that sorts an one-dimensional array of number either ascending or descending. This function sorts using the well known double for..next principle. Script gives an overview of passing parameters and especially passing arrays of numbers to a function.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Sort arrays
' Description:Vbscript function that sorts 
an one-dimensional array 
of number either ascending
or descending.
This function sorts using 
the well known double 
for..next principle. 
Script gives an overview 
of passing parameters 
and especially passing 
arrays of numbers 
to a function.
' By: Rogier Doekes (from psc cd)
'
' Inputs:The function gets as 
input an unsorted 
array, plus 1 for 
ascending sorting 
or something else 
for descending sorting
'
' Returns:The function returns
a sorted array of numbers.
'
' Assumes:Using SQL sorting is not
 a problem. However, for 
those cases when there 
is no SQL to sort with, 
or an array of numbers 
is manipulated requiring 
a sort, this function 
might come in handy. 
Also, VBscript does 
not have a build-in 
sorting function.
'**************************************

'--------Begin Function----
Function fnSort(aSort, intAsc)
 Dim intTempStore
 Dim i, j 
 For i = 0 To UBound(aSort) - 1
 For j = i To UBound(aSort)
 'Sort Ascending
 If intAsc = 1 Then
 If aSort(i) > aSort(j) Then
intTempStore = aSort(i)
aSort(i) = aSort(j)
aSort(j) = intTempStore
 End If 'i > j
 'Sort Descending
 Else
 If aSort(i) < aSort(j) Then
intTempStore = aSort(i)
aSort(i) = aSort(j)
aSort(j) = intTempStore
 End If 'i < j
 End If 'intAsc = 1
 Next 'j
 Next 'i
 fnSort = aSort
End Function 'fnSort
'-------------------------
Dim aUnSort(3), aSorted
aUnSort(0) = 4
aUnSort(1) = 2
aUnSort(2) = 6
aUnSort(3) = 20
'call the function
'second argument:
' * ascending sorted = 1
' * descending sorting = any other character
aSorted = fnSort(aUnSort, 1)
Erase aUnSort


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.