How to loop through a two-dimensional array and alphabetize...

Posted on 2006-04-26
Last Modified: 2012-08-13
I would like to take arrNames(1,x) and alphabetize the entire array by x.

So its a two column array that appears like this:

Username, Full Name
jsmith, John Smith
dwilson, Don Wilson
mjohnson, Michelle Johnson

and I would like to just alphabetize by column two (FullName) so when i loop through the area from 1 to ubound the contents will be in this order:
dwilson, Don Wilson
jsmith, John Smith
mjohnson, Michelle Johnson

Question by:mschwade
    LVL 7

    Expert Comment

    Try this:

    It uses a short JScript server code block to do the actual sorting since it is built into JScript.
    LVL 28

    Expert Comment

    For i = 0 To Ubound(MyArray,2)
        Response.write MyArray(0, i) & " " & MyArray(1, i)
    LVL 28

    Accepted Solution

    Sorting an array in VBScript can be done in many ways. It depends a bit how big the array is, but I'd use the Sort method of a Recordset, and not JavaScript. You don't want to switch to JavaScript just for one function.

    Function Sort2DArrayRS(ByVal aIn)
        Dim aReturn, oRS, i
        If Not IsArray(aIn) Then Exit Function
        Set oRS = Server.CreateObject("ADODB.RecordSet")
        oRS.Fields.Append  "username", 201, 255    ' adLongVarChar
        oRS.Fields.Append  "fullname", 201, 255    ' adLongVarChar
        oRS.CursorLocation = 3                 ' adUseClient
        For i = 0 To Ubound(aIn,2)
            oRS("username") = aIn(0,i)
            oRS("fullname") = aIn(1,i)
        oRS.Sort = "fullname"
        Sort2DArrayRS = oRS.GetRows()
        oRS.Close:Set oRS = Nothing
    End Function

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now