Sorting data after using the GetRows method

Results 1 to 2 of 2

Thread: Sorting data after using the GetRows method

  1. #1
    Steve B Guest

    Default Sorting data after using the GetRows method

    The GetRows method of putting a recordset into an array puts the columns where the rows should be and vice-versa. I need to sort by one of the columns in the recordset, but in the array, that would mean sorting by one of the rows. I've found a couple of programs that will sort by a column, but none that will sort by a row. So, I need to find out how to either reverse the columns and rows in my array, or find a program that sorts by rows. I'm using VBScript with ASP. I can't use the recordset itself, because it takes too much time and I keep getting errors from the server that 2 people are trying to update the same table at the same time. This is because when I used the recordset, I would loop through the recordset 4 times, updating one of the columns and sorting by that column. Thanks for any help you can give.

  2. #2
    Join Date
    Dec 1969

    Default Sorting a TWO dimensional array... usually a mistake.<BR><BR>Why can&#039;t you simply do an ORDER BY in your SQL query and get the array pre-sorted???<BR><BR>(And sorting a recordset after it is loaded is usually a mistake, too, though better than sorting the 2D array in VBS code!)<BR><BR>Anyway, the rows and columns are *NOT* "swapped"--or at least not in any way that matters You&#039;ve just never seen code for doing a sort of a 2D array. If you had, the transformation would be truly trivial.<BR><BR>Look the basic problem is here:<BR><BR>In a sort of a 1-D array, inevitably there comes a time when you have to "swap" elements of the array. That&#039;s usually done something like this:<BR><BR>...<BR>If theArray(i) &#060; theArray(j) Then<BR>&nbsp; &nbsp; temp = theArray(i)<BR>&nbsp; &nbsp; theArray(i) = theArray(j)<BR>&nbsp; &nbsp; theArray(j) = temp<BR>End If<BR>...<BR><BR>But with a 2-D array, you must swap *ALL THE COLUMNS* in order to swap the rows!<BR><BR>So something as ugly as this:<BR><BR>...<BR>If theArray(keycolumn,i) &#060; theArray(keycolumn,j) Then<BR>&nbsp; &nbsp; For c = 0 To UBound(theArray,1) &#039; number of columns<BR>&nbsp; &nbsp; &nbsp; &nbsp; temp = theArray(c,i)<BR>&nbsp; &nbsp; &nbsp; &nbsp; theArray(c,i) = theArray(c,j)<BR>&nbsp; &nbsp; &nbsp; &nbsp; theArray(c,j) = temp<BR>&nbsp; &nbsp; Next<BR>End If<BR>...<BR><BR>Aside from being clumsy to code, that is also VEERRRYYY SLLOOOOWWW. <BR><BR>So you want to avoid it if you possibly can!<BR><BR>And there are ways to avoid it. Such as building a "side array" that you actually sort that is a 1D array with references to the 2D elements. But even it is not pretty.<BR><BR>Are you *sure* you can&#039;t simply do an ORDER BY in your SQL and avoid this crap?<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