Link to home
Start Free TrialLog in
Avatar of Roberto Parker
Roberto ParkerFlag for Mexico

asked on

classic asp sort an array

I create an array in my asp script Array

<%' I need to order the array here before it is output
for Each item In Array
response.write Item & "<br>"
next>
%>

Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Arrays are not easy to work with in vbscript as compared to other languages.  You can use the quicksort function from 4guys as a function, then send the array to the function.

https://www.4guysfromrolla.com/webtech/011001-1.shtml
https://www.4guysfromrolla.com/webtech/012799-2.shtml

I know it sounds so obvious it was probably thought of already, but you can send the unsorted data to the browser. Another option is to let javascript sort sort the data https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
Avatar of Roberto Parker

ASKER

I like the sort option

I do

<%' I need to order the array here before it is output
sort.Array
Array.sort()
for Each item In Array
response.write Item & "<br>"
next
%>

It does not work.Do I have to include some javascript?
 
Take another look at the article. https://www.4guysfromrolla.com/webtech/011001-1.shtml

There are links for using a 1-dimensional and 2-dimensional array.  You didn't state which you are using. 

For whichever version you are using, copy the code exactly as is, get it to work with the sample data so that you know you have it set up properly. Once you have that done, swap out the sample data for your own. If there is an error, most likely it has to do with how the data is set up.

If you find you have an error after going through that exercise, create some sample data that you can post here along with the code you are using. The code you post back here should just be the data, sort function and output to keep it easy.
That link I posted was only for the Bubble Sort, I meant to repost the Simplesort too https://www.4guysfromrolla.com/webtech/012799-2.shtml as I was not sure what you were doing
Is this a vector or n-dimension array?
What is the nature of the data you are sorting? (data type, column, part of a string)
Is the sort key complex or a single key?
Are the items in a single sorting sequence or are some keys sorted in a different order? (asc vs desc)

sort.Array
Array.sort()
As Scott alluded, there is no intrinsic array sorting feature in VBScript.  You will need to add VBS code to your ASP project.  You can use one of the routines he posted links to or write one yourself.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.