Important alert: (current site time 9/20/2014 3:36:44 AM EDT)
 

winzip icon

Multidimensional Array Sort function in VB Script

Email
Submitted on: 7/28/2006 3:38:14 PM
By: Eric Repec InetSolution  
Level: Advanced
User Rating: By 4 Users
Compatibility: ASP (Active Server Pages)
Views: 119353
author picture
(About the author)
 
     This function will sort an array. It will reorder all the information in each row of the array so that the selected column of the array is sorted. The first row of the array is used to decide what variable types are being used for the sort column. I use this in many of my sites where I have to pull back encrypted data from the database. I decrypt the data with an Active X object inside of the ASP script then place all the data into an array. Once the data is in the array I can sort it and display it to the user. Future enhancements to this function will include the ability to select which column of the array you would like to sort on. This function has been very popular so I am keeping an up to date version of it on our business site. See my profile for the link to InetSolution. Thanks Happy coding Eric Repec

 
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.


Other 1 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 Advanced 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

3/21/2001 12:02:54 PMTimothy J. Ward

Could you post a sample of this being used?
(If this comment was disrespectful, please report it.)

 
9/12/2001 2:41:28 PMRachel

Can you please post a sample as to how to use this function..

Thanks.
(If this comment was disrespectful, please report it.)

 
12/10/2001 10:26:05 AMTurbosig

This is awesome... To sort by any other columns, simply pass in another value which represents the column to sort by. Works great. Thanks.
(If this comment was disrespectful, please report it.)

 
11/11/2003 10:21:27 AM

Could you post a sample of this being
used? please!
(If this comment was disrespectful, please report it.)

 
5/27/2004 10:59:24 AM

When I use it it doesn't sort integers properly. It sorts like this:

1
11
2
3
34
4
5
6
65
7
8
9

That's not right is it! :)

Additional variables for DESC or ASC would be cool too. :)

Otherwise it rocks.
(If this comment was disrespectful, please report it.)

 
5/27/2004 1:32:02 PMEric Repec InetSolution

I do have an updated version of this routine if you would like it. The new version take in account of the data type and will fix the problem outlined above.
(If this comment was disrespectful, please report it.)

 
6/21/2004 5:42:51 PM

Hello:
Where we can view the source code for the new version of the sorting Multi-dimension array?
(If this comment was disrespectful, please report it.)

 
7/28/2004 7:49:13 PM

this is excellent!! one question:

in the test loop, what purpose does the following serve?
smallest_value = values(0,j)

it doesn't seem to be used for anything, and then when you loop back through the outer loop, it just gets reassigned.
(If this comment was disrespectful, please report it.)

 
12/10/2004 7:24:37 AM

this is also a great resource for multi-D array's
http://www.aspfree.com/c/a/ASP/How-to-Sort-a-Multi-Dimensional-Array/3/
(If this comment was disrespectful, please report it.)

 
5/18/2006 2:27:55 PMHolzberg

This is a nice function, however if you have the following:
arr(0,0) = "002"
arr(0,1) = "AAA"
it will error on cdbl.
The reason is because only the first value is checked to see what datatype should be used.

Instead, the code should loop through all values and if you have a mix of types then set it to 0 (String) (Hint: once a string or mix is found exit the loop to speed things up)

(If this comment was disrespectful, please report it.)

 
6/25/2006 8:55:50 PMUsing Multidimensional Array Sort function

Can I use this function to sort data with multiple columns in WSH?

It seems to me that this code works with WSH, but I just don't know how to use the function.

I attempted to by doing the following, but it did not work.

Function ArraySort(values(),intSortCol,strDirection)

I have an array with value, and I insert the value like:

Function ArraySort(arrayvalu,3,a) into the first line of the function, but I get an error "Subscript out of range: 'LBound' at the line "min = LBound(values,2)" inside the code.

Could you please let me know why I get the error?

Thanks!


(If this comment was disrespectful, please report it.)

 
2/2/2008 12:53:00 AMErin

Hi there,

Can you please post a sample of how you use this function?

Many thanks!

Erin
(If this comment was disrespectful, please report it.)

 
4/5/2009 2:04:23 PMUsing Multidimensional Array Sort function

Hello,

I tried to sort my multidimensional array (array(row,col)) with your function, but I have a different effect. The ascending becomes descending, and the sort is horizontal instead of vertical. Maybe this function requires a multidimensional array with the array format of array(col,row) instead of array(row,col). If this is the case, how can I modify this function to sort a mmultidimensional array with array(row,col)?

Thanks.
(If this comment was disrespectful, please report it.)

 
10/20/2009 4:17:29 PMTodd Giardina

sorts array
(If this comment was disrespectful, please report it.)

 
4/15/2010 4:38:36 PMWebcube

Seems great, but I must be doing something wrong, cause I only get vb errors.

Could you please show/post a code excample to show how I should call this function from vb script?

Tried several things in calling the functions, like (see below).
should I dim or redim array to store results??? Just can't figure it out
aSortDrives = arraysort(aDrives, 1, "a")
aSortDrives = arraysort aDrives, 1, "a"
aSortDrives = arraysort(aDrives, 1, "a")
aSortDrives = arraysort(aDrives(), 1, "a")

(If this comment was disrespectful, please report it.)

 
4/20/2010 10:30:06 AMWebcube

OK... Figured it out...

Dim aSortDrives 'without ()
aSortDrives = arraysort(aDrives,0,"d")
(If this comment was disrespectful, please report it.)

 

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.