in Web and Tech

Multi-dimensional Array Sorting in ASP

It’s no secret that ASP has no decent built-in function to manipulate arrays, let alone multidimensional arrays.

After wading through a number of articles and forum posts which mostly offers some form of algorithmic exercise based on other languages and what-not (to name a few rather significant ones: aspfree.com, evolt.com, forguysfromrolla.com), I finally encountered a pretty cool hack that was a reply to a forum question by some guy who somehow had the same problem as I did.

In contrast to most solutions offered, it was simple and yet effective. Mind you I tried the first solutions. For some reason, they just didn’t work for me. Either it was not cut out for my requirement or it crashed on me midway, and knowing how helpful error messages are with ASP, and gut me stuck.

The hack made use of the ADO recordset object and took advantage of the methods built into the object.

Well, it was certainly no spoon-feed. The answer did point me to the right direction. I had to again wade through debugging gunk before I finally got it to work. And, as always, the wading taught me a few more things, particularly on the recordset object.

Here’s the final solution:

<%
'define sample array and assign corresponding values
Dim listProj(5,3)

listProj(0,0) = "1-Jan-2004"
listProj(1,0) = "12:00"
listProj(2,0) = "Text One"
listProj(3,0) = 10
listProj(4,0) = 5

listProj(0,1) = "1-Jan-2005"
listProj(1,1) = "2:00"
listProj(2,1) = "Superman"
listProj(3,1) =	5
listProj(4,1) = 10

listProj(0,2) = "1-Jan-2009"
listProj(1,2) = "1:00"
listProj(2,2) = "Hyperbola"
listProj(3,2) = 50
listProj(4,2) = 1

'define the handle for the recordset and set constants for the data type definition
dim rs
Const adDBTimeStamp = 135
Const adVarChar = 200
Const adInteger = 3

'create the recordset object and assign it to the handle
set rs=createobject("adodb.recordset")
'add the fields to the recordset
With rs.Fields
	.Append "fDate",adDBTimeStamp
	.Append "fTime",adDBTimeStamp
	.Append "fText",adVarChar,50
	.Append "fNum1",adInteger
	.Append "fNum2",adInteger
End With
'open the recordset
rs.Open

'create an array for the fields; this will be used when adding new records
arrFields = array("fDate","fTime","fText","fNum1","fNum2")

for i=0 to (Ubound(listProj, 2)-1)
	'the following line will not work for AddNew
        'rs.AddNew Array(listProj(0,i),listProj(1,i),listProj(2,i),listProj(3,i),listProj(4,i))

	'set an array for the corresponding values
	arrVals = array(listProj(0,i),listProj(1,i),listProj(2,i),listProj(3,i),listProj(4,i))
	rs.AddNew arrFields,arrVals
next

'reset the cursor to the first record
rs.MoveFirst 
'now do the sorting
rs.Sort="fDate"

Response.Write rs.RecordCount & "
" 'loop through the recordset starting from the first and display each field name and corresponding value do until rs.EOF 'go through each field on the record for each x in rs.Fields Response.Write(x.name) Response.Write(" = ") Response.Write(x.value & "
") next Response.Write("
") 'move to the next record rs.MoveNext loop 'close the record set rs.close %>

It is important to catch and filter values being entered into the arrVals array. If a value is equal to “”, the following run-time error occurs:

Error Type:
Microsoft Cursor Engine (0x80040E21)
Multiple-step operation generated errors. Check each status value.

The same error as mentioned above may also occur if the value you enter is greater than the size of the field. If a string with a length of 200 is entered into fText, an error occurs.

Write a Comment

Comment