Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

Quick and Dirty ASP Array Sorting....

Rated 4.13 (Ratings: 6) (Add your rating)

Log in to add a comment
(23 comments so far)

Want more?

 
Picture of AnthonyB

Anthony Baratta

Member info | Full bio

User since: July 09, 1999

Last login: October 25, 2006

Articles written: 12

or How I stopped worrying and hacked someone else's code.

One of the most frustrating non-options with VB Scripting is the inability to sort arrays. If you've rolled your own, most people opt for the bubble sort method. This works great until you have a medium to large array. Then it becomes horribly inefficient.

On the 4 Guys From Rolla web site, they have a nifty ASP function published called Quick Sort that can handle multi-dimensional sorting. It's based on the algorithm given in Data Abstractions & Structures using C++ by Mark Headington and David Riley, (pg. 586).

The 4 Guys From Rolla QuickSort is very nice, but has a few short comings. One: It does not offer an option for Ascending/Descending Sorting. Two: It does string compares for all field comparisons. Three: You must have the array structured as Row,Column which is backwards from how ADO returns an array via the getRows function. [rant]MvHO it's ADO that returns the array backwards. Why couldn't MS offer both ways if there was a need to "maintain backwards compatibility" with a historically fubarred thought process??[/rant]

Anywho - I hacked the 4 Guys From Rolla Quick Sort routine (it's nice to be able to stand on the shoulders of giants) to support sorting direction as well as embedded a function that checks to see if the two fields being compared can both be evaluated as Numeric or as Strings (default) and "cloned" the QuickSort routine for the ADO way of array thinking.

The only thing lacking now with QuickSortv2 and QuickSortv2_ADO is a second column sort - but I'll leave that to someone better qualified to implement. ;-)

To utilize the Quick Sort routines, call it this way:

QuickSortADO vecloBoundhiBoundSortFieldSortDir

QuickSort vecloBoundhiBoundSortFieldSortDir

Parameters:
  • vec - array to be sorted

  • loBound &
  • hiBound - are simply the upper and lower bounds of the array's "row" dimension. [ADO = UBound(vec,2), Regular = UBound(vec,1)] It's probably easiest to use the LBound and UBound functions to set these.

  • SortField - The field to sort on (1st dimension value)

  • SortDir - ASC, ascending; DESC, descending

Note: The PrintArray and PrintArrayADO are quick and dirty functions to print out your array in a table structure to quickly evaluate the sorting routine. So at minimum you need three functions: QuickSortADO, SwapRowsADO, and FormatCompare and/or QuickSort, SwapRows, and FormatCompare. Obviously you only need one copy of FormatCompare if you want both QuickSort routines in your library.

***Quick Sort v2***

''' Regular Array Sort

Sub QuickSort(vec,loBound,hiBound,SortField,SortDir)
  '==--------------------------------------------------------==
  '== Sort a multi dimensional array on SortField            ==
  '==                                                        ==
  '== This procedure is adapted from the algorithm given in: ==
  '==    ~ Data Abstractions & Structures using C++ by ~     ==
  '==    ~ Mark Headington and David Riley, pg. 586    ~     ==
  '== Quicksort is the fastest array sorting routine for     ==
  '== unordered arrays.  Its big O is n log n                ==
  '==                                                        ==
  '== Parameters:                                            ==
  '== vec       - array to be sorted                         ==
  '== SortField - The field to sort on (1st dimension value) ==
  '== loBound and hiBound are simply the upper and lower     ==
  '==   bounds of the array's "row" dimension. It's probably ==
  '==   easiest to use the LBound and UBound functions to    ==
  '==   set these.                                           ==
  '== SortDir   - ASC, ascending; DESC, Descending           ==
  '==--------------------------------------------------------==
  if not (hiBound - loBound = 0) then
      Dim pivot(),loSwap,hiSwap,temp,counter
      Redim pivot (Ubound(vec,2))
      SortDir = UCase(SortDir)

      '== Two items to sort
      if hiBound - loBound = 1 then
        if (SortDir = "ASC") then
            if FormatCompare(vec(loBound,SortField),vec(hiBound,SortField)) > FormatCompare(vec(hiBound,SortField),vec(loBound,SortField)) then Call SwapRows(vec,hiBound,loBound)
        else
            if FormatCompare(vec(loBound,SortField),vec(hiBound,SortField)) < FormatCompare(vec(hiBound,SortField),vec(loBound,SortField)) then Call SwapRows(vec,hiBound,loBound)
        end if
      End If

      '== Three or more items to sort
      For counter = 0 to Ubound(vec,2)
        pivot(counter) = vec(int((loBound + hiBound) / 2),counter)
        vec(int((loBound + hiBound) / 2),counter) = vec(loBound,counter)
        vec(loBound,counter) = pivot(counter)
      Next

      loSwap = loBound + 1
      hiSwap = hiBound

      do
        '== Find the right loSwap
        if (SortDir = "ASC") then
            while loSwap < hiSwap and FormatCompare(vec(loSwap,SortField),pivot(SortField)) <= FormatCompare(pivot(SortField),vec(loSwap,SortField))
              loSwap = loSwap + 1
            wend
        else
            while loSwap < hiSwap and FormatCompare(vec(loSwap,SortField),pivot(SortField)) >= FormatCompare(pivot(SortField),vec(loSwap,SortField))
              loSwap = loSwap + 1
            wend
        end if
        '== Find the right hiSwap
        if (SortDir = "ASC") then
            while FormatCompare(vec(hiSwap,SortField),pivot(SortField)) > FormatCompare(pivot(SortField),vec(hiSwap,SortField))
              hiSwap = hiSwap - 1
            wend
        else
            while FormatCompare(vec(hiSwap,SortField),pivot(SortField)) < FormatCompare(pivot(SortField),vec(hiSwap,SortField))
              hiSwap = hiSwap - 1
            wend
        end if
        '== Swap values if loSwap is less then hiSwap
        if loSwap < hiSwap then Call SwapRows(vec,loSwap,hiSwap)
      loop while loSwap < hiSwap

      For counter = 0 to Ubound(vec,2)
        vec(loBound,counter) = vec(hiSwap,counter)
        vec(hiSwap,counter) = pivot(counter)
      Next

      '== Recursively call function .. the beauty of Quicksort
        '== 2 or more items in first section
        if loBound < (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1,SortField,SortDir)
        '== 2 or more items in second section
        if hiSwap + 1 < hibound then Call QuickSort(vec,hiSwap+1,hiBound,SortField,SortDir)
  end if
End Sub  'QuickSort

Sub SwapRows(ary,row1,row2)
  '==------------------------------------------==
  '== This proc swaps two rows of an array     ==
  '==------------------------------------------==

  Dim x,tempvar
  For x = 0 to Ubound(ary,2)
    tempvar = ary(row1,x)
    ary(row1,x) = ary(row2,x)
    ary(row2,x) = tempvar
  Next
End Sub  'SwapRows

function FormatCompare(sOne,sTwo)
  '==------------------------------------------==
  '==  Checks sOne & sTwo, returns sOne as a   ==
  '==  Numeric if both pass isNumeric, if not  ==
  '==  returns sOne as a string.               ==
  '==------------------------------------------==

    if (isNumeric(Trim(sOne)) AND isNumeric(Trim(sTwo))) then
        FormatCompare = CDbl(Trim(sOne))
    else
        FormatCompare = Trim(sOne)
    end if
end function

Sub PrintArray(vec,loRow,hiRow,markCol)
  '==------------------------------------------==
  '== Print out an array  Highlight the column ==
  '==  whose number matches param markCol      ==
  '==------------------------------------------==

  Dim ColNmbr,RowNmbr
  Response.Write "<table border=""1"" cellspacing=""0"">"
  For RowNmbr = loRow to hiRow
    Response.Write "<tr>"
    For ColNmbr = 0 to (Ubound(vec,2) - 1)
      If ColNmbr = markCol then
        Response.Write "<td bgcolor=""FFFFCC"">"
      Else
        Response.Write "<td>"
      End If
      Response.Write vec(RowNmbr,ColNmbr) & "</td>"
    Next
    Response.Write "</tr>"
  Next
  Response.Write "</table>"
End Sub  'PrintArray

***Quick Sort v2 ADO***

''' ADO Array Sort

Sub QuickSortADO(vec,loBound,hiBound,SortField,SortDir)
  '==--------------------------------------------------------==
  '== Sort a multi dimensional array on SortField            ==
  '==                                                        ==
  '== This procedure is adapted from the algorithm given in: ==
  '==    ~ Data Abstractions & Structures using C++ by ~     ==
  '==    ~ Mark Headington and David Riley, pg. 586    ~     ==
  '== Quicksort is the fastest array sorting routine for     ==
  '== unordered arrays.  Its big O is n log n                ==
  '==                                                        ==
  '== Parameters:                                            ==
  '== vec       - array to be sorted                         ==
  '== SortField - The field to sort on (1st dimension value) ==
  '== loBound and hiBound are simply the upper and lower     ==
  '==   bounds of the array's "row" dimension. It's probably ==
  '==   easiest to use the LBound and UBound functions to    ==
  '==   set these.                                           ==
  '== SortDir   - ASC, ascending; DESC, Descending           ==
  '==--------------------------------------------------------==

  if not (hiBound - loBound = 0) then
      Dim pivot(),loSwap,hiSwap,temp,counter
      Redim pivot (Ubound(vec,1))
      SortDir = UCase(SortDir)

      '== Two items to sort
      if hiBound - loBound = 1 then
        if (SortDir = "ASC") then
            if FormatCompare(vec(SortField,loBound),vec(SortField,hiBound)) > FormatCompare(vec(SortField,hiBound),vec(SortField,loBound)) then Call SwapRowsADO(vec,hiBound,loBound)
        else
            if FormatCompare(vec(SortField,loBound),vec(SortField,hiBound)) < FormatCompare(vec(SortField,hiBound),vec(SortField,loBound)) then Call SwapRowsADO(vec,hiBound,loBound)
        end if
      End If

      '== Three or more items to sort
      For counter = 0 to Ubound(vec,1)
        pivot(counter) = vec(counter,int((loBound + hiBound) / 2))
        vec(counter,int((loBound + hiBound) / 2)) = vec(counter,loBound)
        vec(counter,loBound) = pivot(counter)
      Next

      loSwap = loBound + 1
      hiSwap = hiBound

      do
        '== Find the right loSwap
        if (SortDir = "ASC") then
            while loSwap < hiSwap and FormatCompare(vec(SortField,loSwap),pivot(SortField)) <= FormatCompare(pivot(SortField),vec(SortField,loSwap))
              loSwap = loSwap + 1
            wend
        else
            while loSwap < hiSwap and FormatCompare(vec(SortField,loSwap),pivot(SortField)) >= FormatCompare(pivot(SortField),vec(SortField,loSwap))
              loSwap = loSwap + 1
            wend
        end if
        '== Find the right hiSwap
        if (SortDir = "ASC") then
            while FormatCompare(vec(SortField,hiSwap),pivot(SortField)) > FormatCompare(pivot(SortField),vec(SortField,hiSwap))
              hiSwap = hiSwap - 1
            wend
        else
            while FormatCompare(vec(SortField,hiSwap),pivot(SortField)) < FormatCompare(pivot(SortField),vec(SortField,hiSwap))
              hiSwap = hiSwap - 1
            wend
        end if
        '== Swap values if loSwap is less then hiSwap
        if loSwap < hiSwap then Call SwapRowsADO(vec,loSwap,hiSwap)
      loop while loSwap < hiSwap

      For counter = 0 to Ubound(vec,1)
        vec(counter,loBound) = vec(counter,hiSwap)
        vec(counter,hiSwap) = pivot(counter)
      Next

      '== Recursively call function .. the beauty of Quicksort
        '== 2 or more items in first section
        if loBound < (hiSwap - 1) then Call QuickSortADO(vec,loBound,hiSwap-1,SortField,SortDir)
        '== 2 or more items in second section
        if hiSwap + 1 < hibound then Call QuickSortADO(vec,hiSwap+1,hiBound,SortField,SortDir)
  end if
End Sub  'QuickSortADO

Sub SwapRowsADO(ary,row1,row2)
  '==------------------------------------------==
  '== This proc swaps two rows of an array     ==
  '==------------------------------------------==

  Dim x,tempvar
  For x = 0 to Ubound(ary,1)
    tempvar = ary(x,row1)
    ary(x,row1) = ary(x,row2)
    ary(x,row2) = tempvar
  Next
End Sub  'SwapRowsADO


function FormatCompare(sOne,sTwo)
  '==------------------------------------------==
  '==  Checks sOne & sTwo, returns sOne as a   ==
  '==  Numeric if both pass isNumeric, if not  ==
  '==  returns sOne as a string.               ==
  '==------------------------------------------==

    if (isNumeric(Trim(sOne)) AND isNumeric(Trim(sTwo))) then
        FormatCompare = CDbl(Trim(sOne))
    else
        FormatCompare = Trim(sOne)
    end if
end function

Sub PrintArrayADO(vec,loRow,hiRow,markCol)
  '==------------------------------------------==
  '== Print out an array  Highlight the column ==
  '==  whose number matches param markCol      ==
  '==------------------------------------------==

  Dim ColNmbr,RowNmbr
  Response.Write "<table border=""1"" cellspacing=""0"">"
  For RowNmbr = loRow to hiRow
    Response.Write "<tr>"
    For ColNmbr = 0 to Ubound(vec,1)
      If ColNmbr = markCol then
        Response.Write "<td bgcolor=""FFFFCC"">"
      Else
        Response.Write "<td>"
      End If
      Response.Write vec(ColNmbr,RowNmbr) & "</td>"
    Next
    Response.Write "</tr>"
  Next
  Response.Write "</table>"
End Sub  'PrintArray
Mutated into a life-size Dilbert doll, Anthony spends the days wedged into his replica of Cardinal Fang's Comfy Chair coding solutions to the most thorny of internet software problems.

2D, two-column sort for JavaScript

Submitted by endquote on March 1, 2002 - 16:01.

Not entirely relevant, but here's a 2D, 2-column bubble sort that I wrote in JavaScript:

/* sort2D - sort a two-dimensional array by a given column and direction
	arguments:
		arIn - a two-dimentional array
		intPrimary - the first column to sort by
		intSecondary - the second column to sort by (use false or an empty string to sort only by primary)
		boolDirection - the direction to sort (1/true = ascending, 0/false = decending)
	returns:
		a copy of arIn, sorted as specified */
function sort2D(arIn, intPrimary, intSecondary, boolDirection) {
	for(var i=0; i arIn[j][intPrimary].toLowerCase())) { boolSwitchRows = true; }
			}
			if((arIn[i][intPrimary] == arIn[j][intPrimary]) && (intSecondary !== false) && (intSecondary !== "")) {
				if((boolDirection) && (arIn[i][intSecondary].toLowerCase() < arIn[j][intSecondary].toLowerCase())) { boolSwitchRows = true; }
				if((!boolDirection) && (arIn[i][intSecondary].toLowerCase() > arIn[j][intSecondary].toLowerCase())) { boolSwitchRows = true; }
			}
			if(boolSwitchRows) {
				var arTemp = arIn[j];
				arIn[j] = arIn[i];
				arIn[i] = arTemp;
			}
		}
	}
	return(arIn);
}

login or register to post comments

Fix For Date Sorting

Submitted by pbaugher on August 15, 2002 - 20:37.

Replace your FormatCompare function with the following:
function FormatCompare(sOne,sTwo)
	if (isNumeric(Trim(sOne)) AND isNumeric(Trim(sTwo))) then
		FormatCompare = CDbl(Trim(sOne))
	else
		if (isDate(Trim(sOne)) AND isDate(Trim(sTwo))) then
			FormatCompare = CDate(Trim(sOne))
		else
			FormatCompare = Trim(sOne)
		End If
	end if
end function
This will fix the problem with sorting dates in your arrays! :) Thanks for the script, it has come in handy. Hope this contribution helps anyone who ran in to this problem.

login or register to post comments

Out of Stack Space! WAHHHH!

Submitted by rfjason on April 30, 2004 - 11:07.

When sorting an array of 582 records (35 fields), the routine runs out of stack space. I am sad.

login or register to post comments

RE: Out of Stack Space! WAHHHH!

Submitted by AnthonyB on April 30, 2004 - 11:35.

Yup. It's a sad truth that ASP has horrible memory management and arrays that are "too large" will blow up due to the recursive nature of the routine. I've been toying with the idea of converting the target array to XML, sort via XML Object Properties (if possible), then return it back to an array. But I've been migrating to dotNet and will probably not be able to return to this issue at a later time. If someone takes this idea and runs with it - post an article about it.

login or register to post comments

Alternative

Submitted by rfjason on April 30, 2004 - 13:39.

I found an alternate 2-d quick sort routine that limits the size of it's stack pointer to 32 elements. An interesting solution.

login or register to post comments

pl send the link

Submitted by APUTH on June 15, 2004 - 10:05.

Hi rfjason, can you pls send the code or the link? Thanks for your help in advance.

login or register to post comments

I don't remember the link, so here's the code:

Submitted by rfjason on June 17, 2004 - 08:43.

Public Sub QuickSort(aDataOriginal, iIdx, iLb, iUb)

	Dim i
	Dim j
	Dim aData
	
	' Dimension aData the opposite of aDataOriginal
	Redim aData(ubound(aDataOriginal, 2), ubound(aDataOriginal))
	
	' Transpose
	For i = 0 to ubound(aDataOriginal, 2)
		For j = 0 to ubound(aDataOriginal)
			aData(i, j) = aDataOriginal(j, i)
		Next
	Next
	
	Dim lbStack(32)
	Dim ubStack(32)
	Dim sp              ' stack pointer
	Dim iLbx            ' current lower-bound
	Dim iUbx            ' current upper-bound
	Dim m
	Dim p               ' index to pivot
	Dim t               ' temp used for exchanges

	lbStack(0) = iLb
	ubStack(0) = iUb
	sp = 0
	Do While sp >= 0
		iLbx = lbStack(sp)
		iUbx = ubStack(sp)

		Do While (iLbx < iUbx)

			' select pivot and exchange with 1st element
			p = iLbx + (iUbx - iLbx) \ 2

			' exchange iLbx, p
			Swap aData, iLbx, p

			' partition into two segments
			i = iLbx + 1
			j = iUbx
			Do
				Do While i < j
					If aData(iLbx, iIdx) = i
					If aData(j, iIdx) = j Then Exit Do

				' exchange i, j
				Swap aData, i, j

				j = j - 1
				i = i + 1
			Loop

			' pivot belongs in aData[j]
			' exchange iLbx, j
			Swap aData, iLbx, j
			
			m = j

			' keep processing smallest segment, and stack largest
			If m - iLbx  iLbx Then
					lbStack(sp) = iLbx
					ubStack(sp) = m - 1
					sp = sp + 1
				End If
				iLbx = m + 1
			End If
		Loop
		sp = sp - 1
	Loop

	' Transpose Again
	For i = 0 to ubound(aData, 2)
		For j = 0 to ubound(aData)
			aDataOriginal(i, j) = aData(j, i)
		Next
	Next
End Sub

' ======================================================================
'
' Swap()
'
' Used by QuickSort, this sub swaps two rows in a 2 dimensional array
' 
' Parameters:
'
' aData     array to work with
' iRow1     index of first row
' iRow2     index of second row
'
' e.g. Swap(A, 3, 8) ' swap the 4th and 9th rows in A.

Public Sub Swap(aData, iRow1, iRow2)
	Dim i, tmp
	
	' Get values into Tmp
	For i = 0 to Ubound(aData, 2)
		tmp = aData(iRow1, i)
		aData(iRow1, i) = aData(iRow2, i)
		aData(iRow2, i) = tmp
	Next
End Sub
%>

login or register to post comments

Great Code !!!

Submitted by APUTH on June 18, 2004 - 08:00.

Thank you so much. it helped a lot.

login or register to post comments

HELP ...... Question ... ?

Submitted by KINGKONG on June 23, 2004 - 13:30.

If I want sort by Third column of my array called "myArray. So, what is the SortField in my array as the following ouput. I can't figerure out!

Call QuickSort(myArray,lbound(myArray),ubound(myArray),???????,"ASC")

#0435.724943894311166BCE PlaceToronto CanadaZZ
#176102824.6180284266532Bank One BuildingFort WorthTX
#277562797.594375114650233Highland Village Banking CenterHighland VillageTX
#375093789.130513466950234Preston & Spring Creek BranchPlanoTX
#478744926.533761645050235Stassney HeightsAustinTX
#576092806.950715904150236Southlake Banking CenterSouthlakeTX
#676244811.383767583350237Keller Banking CenterKellerTX
#780030916.114431523150238Broomfield Banking Center-128th ZuniWestminsterCO
#880128-6734927.133864733850239Ken CarylLittletonCO
#980126919.344800327150240Highlands Ranch BranchLittletonCO
#1080501917.940404888450241LongmontLongmontCO

login or register to post comments

syntax is:

Submitted by rfjason on June 23, 2004 - 13:59.

QuickSort(Array, SortColumn, LowerBoundOfArray, UpperBoundOfArray)

Example:

Call QuickSort(MyArray, 3, LBound(myArray), UBound(myArray))

login or register to post comments

Question

Submitted by KINGKONG on June 24, 2004 - 06:28.

 

Any idea I can't sort the red highlight column successful.
Call QuickSort(MyArray, , LBound(myArray), UBound(myArray))

#0 435.7249438943 11166 BCE Place Toronto Canada ZZ
#1 76102 824.6180284266 532 Bank One Building Fort Worth TX
#2 77562 797.5943751146 50233 Highland Village Banking Center Highland Village TX
#3 75093 789.1305134669 50234 Preston & Spring Creek Branch Plano TX
#4 78744 926.5337616450 50235 Stassney Heights Austin TX
#5 76092 806.9507159041 50236 Southlake Banking Center Southlake TX
#6 76244 811.3837675833 50237 Keller Banking Center Keller TX
#7 80030 916.1144315231 50238 Broomfield Banking Center-128th Zuni Westminster CO
#8 80128 927.1338647338 50239 Ken Caryl Littleton CO
#9 80126 919.3448003271 50240 Highlands Ranch Branch Littleton CO
#10 80501 917.9404048884 50241 Longmont Longmont CO

login or register to post comments

Follow up

Submitted by KINGKONG on June 24, 2004 - 06:32.

I mean using
Call QuickSort(MyArray, 3 , LBound(myArray), UBound(myArray)) instead

login or register to post comments

Hmm.

Submitted by rfjason on June 24, 2004 - 09:48.

I can't see your red highlight, so I'm assuming you mean the "#####" values. QuickSort treats everything as strings, so "532" will get sorted to the end. When you attempt a sort, do you get unexpected results, no change, or an error?

login or register to post comments

Thanks rfjason

Submitted by KINGKONG on June 24, 2004 - 10:10.

rfjason:

Thanks for input!

I mean the column with 435.7249438943 (long int). Actually, this is big array gererate from database around 2400 reocrds with 7 columns.

It didn't sort I expect, but its without any error. But as you told me, Quicksort only works for a strings only. So it may not work for sorting those long int number in my case.

I have this arraysort & it works for me. But it take too much times if you have a big array like me.

function arraysort(values())
Dim i
Dim j
Dim smallest_value
Dim smallest_j
dim min
dim m
dim temp

min = lbound(values,2)
max = ubound(values,2)
For i = min To max
smallest_value = values(0,i)
smallest_j = i
For j = i + 1 To max
' See if values(j) is smaller. changed to strComp to work with strings.
'If strComp(values(0,j),smallest_value,vbTextCompare) = -1 Then
If cdbl(values(0,j)) < cdbl(smallest_value) and Len(values(0,j)) 0 Then
' Save the new smallest value.
smallest_value = values(0,j)
smallest_j = j
End If
Next 'j
If smallest_j i Then
' Swap items i and smallest_j.
for intA = 0 to ubound(values,1)
temp = values(intA,smallest_j)
values(intA,smallest_j) = values(intA,i)
values(intA,i) = temp
next 'intA
End If
Next 'i
arraysort = values
End function

login or register to post comments

String / Number / Date....

Submitted by AnthonyB on June 24, 2004 - 10:17.

The code as originally designed did sort via number and string earlier in this thread someone offered an update for Dates. If the compare routine is still intact from all this wonderful hacking - it should still work. If not - check out the original and updated FormatCompare.

login or register to post comments

Thanks ...work now!

Submitted by KINGKONG on June 24, 2004 - 10:33.

Hello all:

I am using ***Quick Sort v2 ADO***, it works now since my array generate from Database!

Thanks all for help & advice!

Best regards!
KingKong

login or register to post comments

QuickSort and Extra Row

Submitted by ladysharra on October 12, 2004 - 09:01.

When I run QuickSort I always get an extra row and the beginning when doing an ASC and an extra row at the end doing a DESC. I run from lbound to ubound. I can create a workaround, was just wondering if anyone else had this problem. The extra row has 0s in the numerical fields and blanks in the string fields.

login or register to post comments

QuickSort and Extra Row

Submitted by AnthonyB on October 12, 2004 - 09:17.

I haven't used this function is a while, but I never saw this behaviour while using it. Check your array and make sure you don't have a blank row in it before feeding it to the function.

login or register to post comments

QuickSort and Extra Row

Submitted by ladysharra on October 12, 2004 - 09:31.

ack... yep. I wish MS would stick with either 1 or 0 for a starting point and use it everywhere. Nice code by the way, tks!

login or register to post comments

sample code for array population?

Submitted by ljp007 on November 14, 2004 - 17:22.

Does anyone have any sample code on how you would fill an array with 5 columns (or so) of data from database -- assuming you wont know how many rows of data will be populating the array (dynamic array creation)? Any help would be appreciated. thanks, lance

login or register to post comments

sample code for array population?

Submitted by AnthonyB on November 14, 2004 - 20:00.

getRows is your friend. Google Search (ASP, ADO, getRows) Have fun.

login or register to post comments

Awesome Routine!

Submitted by windblown on November 17, 2004 - 13:37.

Just wanted to drop a thank you to Anthony and everyone else that's worked on this routine. It fits the bill for the project I'm in the middle of perfectly! These routines will definitely be a permanent part aof my classic ASP arsenal.

login or register to post comments

function that uses QuickSortADO to sort on multiple dimensions

Submitted by janders4 on January 15, 2008 - 22:21.

I wrote this to use QuickSortADO to sort an array on multiple dimensions: ------------------------------- sub QuickSortMultipleDimensionsAdo(vec,primarySortField,primarySortDir,secondarySortField,secondarySortDir) dim vecCopy, copyRowCounter, rowCounter, colCounter, prevPrimaryValue, rowMax, rowMin, colMin, colMax, secondaryRowCounter, secondaryRowMax rowMin = LBound(vec, 2) rowMax = UBound(vec, 2) colMin = LBound(vec, 1) colMax = UBound(vec, 1) Call QuickSortADO (vec, rowMin, rowMax, primarySortField, primarySortDir) redim vecCopy(colMax, rowMax) secondaryRowCounter = 0 copyRowCounter = rowMin for rowCounter = rowMin to rowMax if rowCounter = rowMin or prevPrimaryValue vec(primarySortField, rowCounter) then 'copy all rows with current primary value from secondary to vecCopy if rowCounter rowMin then 'crop previous secondary array and copy contents redim preserve arrSecondary(colMax, secondaryRowCounter) secondaryRowMax = secondaryRowCounter - 1 Call QuickSortADO (arrSecondary, 0, secondaryRowMax, secondarySortField, secondarySortDir) for secondaryRowCounter = 0 to secondaryRowMax for colCounter = colMin to ColMax vecCopy(colCounter,copyRowCounter) = arrSecondary(colCounter, secondaryRowCounter) next copyRowCounter = copyRowCounter + 1 next end if secondaryRowCounter = 0 redim arrSecondary(colMax,rowMax) prevPrimaryValue = vec(primarySortField, rowCounter) end if 'new value for colCounter = colMin to colMax arrSecondary(colCounter, secondaryRowCounter) = vec(colCounter, rowCounter) next secondaryRowCounter = secondaryRowCounter + 1 next redim preserve arrSecondary(colMax, secondaryRowCounter + 1) secondaryRowMax = secondaryRowCounter Call QuickSortADO (arrSecondary, 0, secondaryRowMax, secondarySortField, secondarySortDir) for secondaryRowCounter = 0 to secondaryRowMax if copyRowCounter

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.