Sorting a multidimensional array in descending num

Results 1 to 2 of 2

Thread: Sorting a multidimensional array in descending num

  1. #1
    Join Date
    Dec 1969

    Default Sorting a multidimensional array in descending num

    I&#039;m kinda new to ASP (only about a year of writing it day to day) and have been severely 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>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. #2
    Join Date
    Dec 1969

    Default RE: Sorting a multidimensional array in descending

    How big is this array???<BR><BR>Oh, never mind... This is a pretty crappy way to do a sort of a VBS 2D array. Because you have to swap all the silly elements every time you do a row swap.<BR><BR>Much better is to use an auxilliary array that contains indices into the main array and then swap just the indices.<BR><BR>But never mind...<BR><BR>Look, all you need to do is swap the data comparisons that are done: &#062; becomes &#060;, &#060;= becomese &#062;=, etc. That&#039;s all.<BR><BR>Just don&#039;t get the data comparisons confused with the loop element comparisons.<BR><BR>I *think* (not sure!) I&#039;ve identified all the lines to be changed here:<BR><BR>************<BR>if vec(loBound,SortField) &#060; vec(hiBound,SortField) then Call SwapRows(vec,hiBound,loBound)<BR><BR>&#039; note that I do *NOT* change loSwap &#060; hiSwap!<BR>while loSwap &#060; hiSwap and vec(loSwap,SortField) &#062;= pivot(SortField)<BR><BR>while vec(hiSwap,SortField) &#060; pivot(SortField)<BR><BR>*****************<BR><BR>T he names loSwap and hiSwap are kind of backwards now, but they&#039;re just names.<BR><BR>If you have less than about 60 or 80 rows in this array, consider using something a lot simpler. If you have more than 200 rows, consider using something just a tiny bit more comples.<BR><BR>If you can *possibly* do the sort in SQL instead of in VBS code, DO IT!<BR><BR>

Posting Permissions

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