Sorting a multidimensional array in descending num

# Thread: Sorting a multidimensional array in descending num

1. Junior Member
Join Date
Dec 1969
Posts
3

## Sorting a multidimensional array in descending num

Im kinda new to ASP (only about a year of writing it day to day) and have been severly stuck lately. I am trying to sort a multidimensional array in descending numeric order. Below is the code that I am using (thoughtfully provided to the community by Scott Mitchell)and it works just fine except I can&#039;t figure out to reverse the order of the sort. Any help would be greatly appreciated.<BR><BR><BR>Sub SwapRows(ary,row1,row2)<BR> &#039;== This proc swaps two rows of an array <BR> Dim x,tempvar<BR> For x = 0 to Ubound(ary,2)<BR> tempvar = ary(row1,x) <BR> ary(row1,x) = ary(row2,x)<BR> ary(row2,x) = tempvar<BR> Next<BR>End Sub &#039;SwapRows<BR><BR>Sub QuickSort(vec,loBound,hiBound,SortField)<BR><BR> &#039;==--------------------------------------------------------==<BR> &#039;== Sort a 2 dimensional array on SortField ==<BR> &#039;== ==<BR> &#039;== This procedure is adapted from the algorithm given in: ==<BR> &#039;== ~ Data Abstractions & Structures using C++ by ~ ==<BR> &#039;== ~ Mark Headington and David Riley, pg. 586 ~ ==<BR> &#039;== Quicksort is the fastest array sorting routine for ==<BR> &#039;== unordered arrays. Its big O is n log n ==<BR> &#039;== ==<BR> &#039;== Parameters: ==<BR> &#039;== vec - array to be sorted ==<BR> &#039;== SortField - The field to sort on (2nd dimension value) ==<BR> &#039;== loBound and hiBound are simply the upper and lower ==<BR> &#039;== bounds of the array&#039;s 1st dimension. It&#039;s probably ==<BR> &#039;== easiest to use the LBound and UBound functions to ==<BR> &#039;== set these. ==<BR> &#039;==--------------------------------------------------------==<BR><BR> Dim pivot(),loSwap,hiSwap,temp,counter<BR> Redim pivot (Ubound(vec,2))<BR><BR> &#039;== Two items to sort<BR> if hiBound - loBound = 1 then<BR> if vec(loBound,SortField) &#062; vec(hiBound,SortField) then Call SwapRows(vec,hiBound,loBound)<BR> End If<BR><BR> &#039;== Three or more items to sort<BR> <BR> For counter = 0 to Ubound(vec,2)<BR> pivot(counter) = vec(int((loBound + hiBound) / 2),counter)<BR> vec(int((loBound + hiBound) / 2),counter) = vec(loBound,counter)<BR> vec(loBound,counter) = pivot(counter)<BR> Next<BR><BR> loSwap = loBound + 1<BR> hiSwap = hiBound<BR> <BR> do<BR> &#039;== Find the right loSwap<BR> while loSwap &#060; hiSwap and vec(loSwap,SortField) &#060;= pivot(SortField)<BR> loSwap = loSwap + 1<BR> wend<BR> &#039;== Find the right hiSwap<BR> while vec(hiSwap,SortField) &#062; pivot(SortField)<BR> hiSwap = hiSwap - 1<BR> wend<BR> &#039;== Swap values if loSwap is less then hiSwap<BR> if loSwap &#060; hiSwap then Call SwapRows(vec,loSwap,hiSwap)<BR><BR><BR> loop while loSwap &#060; hiSwap<BR> <BR> For counter = 0 to Ubound(vec,2)<BR> vec(loBound,counter) = vec(hiSwap,counter)<BR> vec(hiSwap,counter) = pivot(counter)<BR> Next<BR> <BR> &#039;== Recursively call function .. the beauty of Quicksort<BR> &#039;== 2 or more items in first section<BR> if loBound &#060; (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1,SortField)<BR> &#039;== 2 or more items in second section<BR> if hiSwap + 1 &#060; hibound then Call QuickSort(vec,hiSwap+1,hiBound,SortField)<BR><BR>E nd Sub &#039;QuickSort

2. Senior Member
Join Date
Dec 1969
Posts
1,274

FAQ #110

3. Junior Member
Join Date
Dec 1969
Posts
3

## RE: Please don't crosspost <eop>

OOPS so sorry

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•