Solved

Sort multidimensional array in ASP

Posted on 2004-08-25
14
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +5
14 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

718 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