• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 765
  • Last Modified:

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

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

Thanks!
Matt
0
mschwade
Asked:
mschwade
  • 2
1 Solution
 
timkerCommented:
Try this: http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=89

It uses a short JScript server code block to do the actual sorting since it is built into JScript.
0
 
sybeCommented:
<%
For i = 0 To Ubound(MyArray,2)
    Response.write MyArray(0, i) & " " & MyArray(1, i)
Next
%>
0
 
sybeCommented:
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
    oRS.Open
    For i = 0 To Ubound(aIn,2)
        oRS.AddNew
        oRS("username") = aIn(0,i)
        oRS("fullname") = aIn(1,i)
        oRS.Update
    Next
    oRS.Sort = "fullname"
    Sort2DArrayRS = oRS.GetRows()
    oRS.Close:Set oRS = Nothing
End Function
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now