?
Solved

Sort multidimensional array in ASP

Posted on 2004-08-25
14
Medium Priority
?
326 Views
Last Modified: 2009-12-16
Hi,

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.

thanks
0
Comment
Question by:brucegm
  • 2
  • 2
  • 2
  • +5
11 Comments
 
LVL 4

Expert Comment

by:jcrumble
ID: 11892823
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?
0
 

Author Comment

by:brucegm
ID: 11892949
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.

0
 

Expert Comment

by:minucy
ID: 11893990
What you mean by " according to three of the columns "? Can you provide a short example of what you want to achieve?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Walter Ritzel
ID: 11894582
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.

Regards,

Walter.
0
 
LVL 4

Expert Comment

by:jcrumble
ID: 11894615
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.

Thanks,
Jeff
0
 
LVL 5

Expert Comment

by:webtrans
ID: 11896115
what about u store the data in a tmp table and then delete the info in it?
0
 
LVL 3

Accepted Solution

by:
passmark earned 1000 total points
ID: 11897474
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
    do
        distance = distance * 3 + 1
    loop until (distance > num)
    do
        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
            loop
            array(1, index2) = value
            array(0, index2) = value0
            array(2, index2) = value2
            array(3, index2) = value3
            array(4, index2) = value4
            array(5, index2) = value5
        next
    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.

David
0
 

Author Comment

by:brucegm
ID: 11899813
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.

thanks
0
 
LVL 4

Expert Comment

by:sgalzin
ID: 11911059
Hi,

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 ...

Stephane.
0
 
LVL 4

Assisted Solution

by:willcode4coffee
willcode4coffee earned 1000 total points
ID: 12202308
Here is an article talking about sorting multi-dimensional arrays in .Net:

http://www.devx.com/dotnet/Article/21089/1954?pf=true


Hope this is what you are looking for.

They also have sampe code for download.

M@
0
 
LVL 3

Expert Comment

by:passmark
ID: 12391406
IMHO, a valid answer with source code was provided.

David
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question