Products
Services
Contact Us

Scripts: Asp :: Asp arrays :: Library Article #14

Developer's Section

Sorting a multidimensional array in ASP
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (3969)
Favorites:

In this tutorial we will be learning how to sort a multidimensional array using ASP. The function we will use takes as input an array and the corresponding argument index to sort.

You can create a Bubble Sort algorithm capable of sorting a multidimensional array in classic ASP. In the example below, we create an sql statement that pulls addresses from a city area table. This will give us a list of all distinct area codes from a region. Then, we populate a multidimensional array with the information from the record set. At this step we take the address field and extract the address number. After that we use a simple Bubble Sort algorithm to sort the addresses that belong to the same area code by address name and then by address number.

Example:

 Code Snippet 1

Dim mySql, RecordSet, dbConnectionStr
Dim mySql2, RecordSet2
Dim mySql3, RecordSet3

'Set the db Connection string to your application connection string
dbConnectionStr = "Driver={MySQL ODBC 3.51 Driver}; " &_
                          "Port=3306; "                                            &_
                          "Server=111.11.00.1; "                               &_
                          "uid=testuser; "                                          &_
                          "pwd=connectionPass; "                             &_
                          "database=newDB01; "                              &_
                          "OPTION=4;"

if request("streetnumber") = "true" then
  mySql  = "SELECT distinct areacode FROM citi_areas "
  mySql  = mySql & "order by areacode, address1"
end if

Set RecordSet = Server.CreateObject("ADODB.RECORDSET")
RecordSet.Open mySql , dbConnectionStr  

if not RecordSet.EOF then

  do until  RecordSet.EOF

    'get number of units that belong to the same street
    dim adr_group_count : adr_group_count = 0

    Set RecordSet2 = Server.CreateObject("ADODB.RECORDSET")

   mySql2 = "SELECT count(*) as adr_count FROM citi_areas " &_
                    " where areacode = '" & RecordSet("areacode") & "' "

    RecordSet2.Open mySql2 , dbConnectionStr

    if not RecordSet2.EOF then
      adr_group_count = RecordSet2("adr_count")
    end if

    ReDim adrArr(3,adr_group_count) ' size of array has been calculated

    Set RecordSet3 = Server.CreateObject("ADODB.RECORDSET")
    mySql3 = "SELECT * FROM citi_areas where " &_
             "areacode = '" & RecordSet("areacode") & "' " 

    mySql3 = mySql3 & "order by address1"

    RecordSet3.Open mySql3 , dbConnectionStr

    'populate array
    Dim adrArrCounter : adrArrCounter = 0

    if not RecordSet3.EOF then
      do until RecordSet3.EOF
        adrArr(0,adrArrCounter) = RecordSet3("areacode")
        adrArr(1,adrArrCounter) = RecordSet3("account_number")
        adrArr(2,adrArrCounter) = RecordSet3("account_resident_name")

        'sort field below
        Dim addressParts : addressParts = RecordSet3("address1")
        'find the street number on the unitnumber DB field
        Dim findStreetNumber : findStreetNumber = 0  
        if trim(addressParts) <> "" then
         Dim addressArr, p
         addressArr=split(addressParts," ")
         For p = 0 to UBound(addressArr)
           if IsNumeric(addressArr(p)) then
              findStreetNumber = CLNG(addressArr(p))
           end if
         Next
        end if

        adrArr(3,adrArrCounter) = findStreetNumber
        adrArrCounter = adrArrCounter + 1
        RecordSet3.movenext
      loop
    end if

    RecordSet3.close
    set RecordSet3 = nothing

    'sort array 
    'Note: we pass the argument 3 as the sort argument
    Dim sortedArray 
    sortedArray  = arraySort( adrArr, 3)

    'display sorted array by address number

    Response.Write("<form name=""sortedArrOptions"" action=""myscript.aspx"">)

    Response.Write("<select name=""sortedAddresses"">")    

    Dim z : z = 0
    For z = 0 to UBound(sortedArray ,2)
      if trim(sortedArray(0,z)) <> "" then 
      %>
      <option value="<%=sortedArray (0,z)%>" id="a<%=i%>" >
      <%=sortedArray (2,z)%></option>
      <%
      i = i + 1
      end if
    Next

    Response.Write("</select>") 

    RecordSet.movenext

  loop

end if

RecordSet.close
set RecordSet = nothing



'>>>>>>>>>>>>>>>><<<<<<<<<<<<<<
' sort multidimensional array of values
'>>>>>>>>>>>>>>>><<<<<<<<<<<<<<
Function arraySort( arToSort, sortBy)

  Dim c, d, e, smallestValue, smallestIndex, tempValue

  For c = 0 To uBound( arToSort, 2 ) - 1
    smallestValue = CLNG( "0" & arToSort( sortBy, c ))
    smallestIndex = c

    For d = c + 1 To uBound( arToSort, 2 )

      if CLNG("0" & arToSort( sortBy, d )) < smallestValue   Then
        smallestValue = CLNG( "0" & arToSort( sortBy, d ))
        smallestIndex = d
      End if

    Next

    if smallestIndex <> c Then 'swap
      For e = 0 To uBound( arToSort, 1 )
        tempValue = arToSort( e, smallestIndex )
        arToSort( e, smallestIndex ) = arToSort( e, c )
        arToSort( e, c ) = tempValue 

      Next
    End if

  Next

  arraySort = arToSort

End Function



See other Scripts in Asp arrays

Submit Your Scripts:


System Message:
  • Your ASP & ASP.NET script has been sent. Please allow 48 hours for processing.


If you would like to have your ASP & ASP.NET scripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]