Sorta a sort Question

# Thread: Sorta a sort Question

1. Senior Member
Join Date
Dec 1969
Posts
1,362

## Sorta a sort Question

Am I the first person<BR><BR>looking for a vbscript sort algorithm that sorts the second dimension of a 2-d array?<BR><BR>It would be extremely hard to believe, but I am starting to become a believer. <BR><BR>

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

## Wow! I am in name shock!

Haven&#039;t seen that one for awhile.

3. Senior Member
Join Date
Dec 1969
Posts
11,334

## RE: Sorta a sort Question

There&#039;s one on the 4guys site that gives you the code to sort a 2 d array, I think.

4. Senior Member
Join Date
Dec 1969
Posts
1,362

## RE: Sorta a sort Question

Hence i said 2nd dimension<BR><BR>it, for some reason sorts the 1st dimension<BR><BR>as in the getrows scenario, it shuffles the columns not the records.<BR><BR>I did a google search a few diff&#039;t ways, going to have to roll my own, which sadly enough, is going to be difficult, cause its the weekend.(almost)

5. Senior Member
Join Date
Dec 1969
Posts
1,362

## Throwback Day

a

6. Senior Member
Join Date
Dec 1969
Posts
898

## RE: Sorta a sort Question

you should be able to modufy the 2d sort easily enough<BR><BR>For the swaprows change to swap columns by loping through records for given colm names etc

7. Heaven's Martini Guest

## Did that already<eop>

(already was mentioned, by you i think)<BR><BR>but I have to rewrite the thing. <BR>(I need to do it when i have time to think about it)<BR><BR>[The for loops screwup, amongst other things. ]

8. Senior Member
Join Date
Dec 1969
Posts
898

## RE: Did that already<eop>

Yeah I never did look at the actual sorting algorithm, I assumed you passed it a vector of items or something that then sorted their ordinal positions according to the item. Like that makes any sense, best of luck and if you do figure it out please post it up here so we can all marvel at your intelligence as well as steal your code for our own use, hehe.

9. Senior Member
Join Date
Dec 1969
Posts
1,362

## Guess I got

you buffalo&#039;d..<BR><BR>"intelligence?"<BR><BR>

10. Senior Member
Join Date
Dec 1969
Posts
96,118

## ### Why is the *algorithm* different? ###

The algorithm *has* to be the same. It&#039;s just the actual code that gets tweaked. The only "trick" to it is that you have to swap all the columns instead of a single field.<BR><BR>But...<BR><BR>But it *might* be faster--especially if you have lots of columns--to instead sort a one-D "side" array that "references" the original.<BR><BR>Something like this:<BR><BR>&#060;%<BR>twoD = ... however you get it ...<BR>maxrow = UBound(twoD,2) <BR>Dim oneD()<BR>ReDim oneD( maxrow )<BR>For i = 0 To maxrow<BR>&nbsp; &nbsp; oneD(i) = i<BR>Next<BR>%&#062;<BR><BR>Now, you sort the oneD array...*EXCEPT*...wherever in your code you *had* something like:<BR><BR>If ar(i) &#062; ar(j) Then<BR>&nbsp; &nbsp; temp = ar(i)<BR>&nbsp; &nbsp; ar(i) = ar(j)<BR>&nbsp; &nbsp; ar(j) = temp<BR>End If<BR><BR>You change it to this:<BR><BR>If twoD(KEYCOLUMN,oneD(i)) &#062; twoD(KEYCOLUMN,oneD(j) Then<BR>&nbsp; &nbsp; temp = oneD(i)<BR>&nbsp; &nbsp; oneD(i) = oneD(j)<BR>&nbsp; &nbsp; oneD(j) = temp<BR>End If<BR><BR>See the point? The swap is still done the same way, but the comparison compares the *REFERENCED* rows in the original twoD array instead of the row numbers (which is all the oneD array holds).<BR><BR>Now, when you are done, to spit things out in sequence, you do:<BR><BR>For i = 0 To maxrow<BR>&nbsp; &nbsp; row = oneD(i)<BR>&nbsp; &nbsp; Response.Write "whatever " & twoD(someColumn,row) & ...<BR>Next<BR><BR>That is, you *indirectly* get the row to use when accessing the twoD array by looking at the elements of the oneD, in order.<BR><BR>Does this make sense? And simplify the conversion of the code? <BR><BR><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
•