Sort multidimensional array in ASP


I have a multidimensional array (28 columns, unlimited rows) which I am trying to sort. I have tried various scripts which I have found on the internet but so far have nothing that actually works. Any help wpuld be much appreciated.

Who is Participating?
passmarkConnect With a Mentor Commented:
ASP doesn't have a sort function for arrays built in (and how stupid is that!). Here is an example sort in ASP.

Sub ShellSort(array)
    last = UBound(array, 2)
    first = LBound(array, 2)
    num = last - first + 1
    ' find the best value for distance
        distance = distance * 3 + 1
    loop until (distance > num)
        distance = distance \ 3
        for index = (distance + first) to last
            value = array(1, index)
            value0 = array(0, index)
            value2 = array(2, index)
            value3 = array(3, index)
            value4 = array(4, index)
            value5 = array(5, index)
            index2 = index
            do while (index2 - distance => first)
                if (array(2, index2 - distance) > value2) then
                    exit do
                end if
                if (array(2, index2 - distance) = value2) then
                    if (array(1, index2 - distance) >= value) then
                        exit do
                    end if
                end if
                array(0, index2) = array(0, index2 - distance)
                array(1, index2) = array(1, index2 - distance)
                array(2, index2) = array(2, index2 - distance)
                array(3, index2) = array(3, index2 - distance)
                array(4, index2) = array(4, index2 - distance)
                array(5, index2) = array(5, index2 - distance)
                index2 = index2 - distance
            array(1, index2) = value
            array(0, index2) = value0
            array(2, index2) = value2
            array(3, index2) = value3
            array(4, index2) = value4
            array(5, index2) = value5
    loop until distance = 1
End Sub

This is (obviously) a non-recursive shell sort of a two dimension array with 5 columns and unlimited rows, and it does work.

IMHO: ASP is a terrible language. If you can switch to another language (SQL, PHP, .NET, etc..) it would be better.

Depending on the functionality you are after you might have better luck using XML.  If your primarily dealing with the information on the client side (ie select a value in one drop down list to populate the values in a secondary drop down list etc), you could empty everything into a JavaScript Array that you parse using substrings on the client side when drop downs are selected.  However, based on the size of your array I hope this is not the scenario.  What exactly are you trying to do with your an array of that size?
brucegmAuthor Commented:
I'm getting a few hundred rows of data (all 28 columns) and need to order them according to three of the columns and then generate an ordered XML output for import into a different application. The client won't actually see any of this, its all serverside.

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

What you mean by " according to three of the columns "? Can you provide a short example of what you want to achieve?
Walter RitzelSenior Software EngineerCommented:
In this case, why you dont get from database in order and then export directly the recordset to xml? I think this the best way.


Is this a SQL based application?  If so you might try posting back to the same page with variable indicating how you wan the output sorted and regenerate your array based on an order by created when you have the columns you want to sort by (ie select ... order by parm1, parm2, parm3).  You can find many examples of this type of coding on various sites when reordering columns in html table like reports when you click on the column title.  

Please provide as mush detail as possible and more of an explaination of what your doing with the array.  Understanding that your going to XML, I still don't understand the purpose behind the array.  Being all server-side is the order you want consistent or will it be changed dynamically?  What is actually displayed to the client if not the information you are retrieving and how are they actually interacting with this ASP page to produce the array?

Any info you can provide would be great.

what about u store the data in a tmp table and then delete the info in it?
brucegmAuthor Commented:
Thanks Passmark. I'll try out this code and see if it works for me. Agreed on the ASP front though - it's ridiculous that there is no sort function. If i could use another language I would, but we are using cobol to extract the data off mpe and its being returned to me totally unsorted. Not ideal, but I'll try and get something to work.


I believe passmark is right about changing languages : I'm new to PHP but when reading the manual I saw there is a function called array_multisort which seems to do exactly what you want ! So maybe changing the server-side scripting engine from ASP to PHP would help ...

willcode4coffeeConnect With a Mentor Commented:
Here is an article talking about sorting multi-dimensional arrays in .Net:

Hope this is what you are looking for.

They also have sampe code for download.

IMHO, a valid answer with source code was provided.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.