Reordering A 2D Array

# Thread: Reordering A 2D Array

1. rye
Member
Join Date
Dec 1969
Posts
67

## Reordering A 2D Array

Okay, imagine, if you will, a database table containing a list of 100 or so advertisers. At the moment im using GetRows to return all the records in alphabetical order. Ah, but theres a problem with this...<BR><BR>Mr Advertiser A will always be at the top, and Miss Advertiser Z will always be at the bottom. Now i could just randomise the array, which would make the advertisers happy, but because you loose the ordering, you make it harder for visitors to find a specific record. So...<BR><BR>What i would ideally like to do is this: To display the array alphabetically, but to begin the list at a random letter and then cycle the array back to the records we missed, ie...<BR><BR>MNOPQRSTUVWXYZABCDEFGHIJKL or TUVWXYZABCDEFGHIJKLMNOPQRS<BR><BR>Does this make sense? If so, does anyone have any ideas how i can do this?

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

## RE: Reordering A 2D Array

I don&#039;t understand this part:<BR><BR>&#062; but because you loose the ordering, you make it <BR>&#062; harder for visitors to find a specific record<BR><BR>Huh? Why would the visitors be *looking* for a specific record?<BR><BR>Do you mean that you show all 100 advertisers to each visitor, every time? Instead of randomly picking one or two advertisers for each visitor??<BR><BR>Anyway...<BR><BR>The bit with starting at an initial letter or position in the array would be easy. Let&#039;s do it with position instead of letter (it&#039;s easier), okay?<BR><BR>&#060;%<BR>....<BR>records = RS.GetRows<BR><BR>recMax = UBound( records, 2 )<BR>Randomize<BR>startAt = Int( Rnd * ( recMax+1) ) &#039; get 0 to recMax<BR>For rec = startAt To recMax<BR>&nbsp; &nbsp; ... show a record ...<BR>Next<BR>For rec = 0 To startAt-1<BR>&nbsp; &nbsp; ... show a record ...<BR>Next<BR>...<BR>%&#062;<BR><BR>If you make the "show a record" code into a SUB or FUNCTION, then you don&#039;t even have to copy/paste that code into each loop.<BR><BR><BR>

3. rye
Member
Join Date
Dec 1969
Posts
67

## RE: Reordering A 2D Array

Thanks for that. Sometimes the simple answers are the ones you just cant see. Thanks. :)<BR><BR>And yes, in this case i need to pull back a complete list of advertisers (accommodation providers) each and every time, and then display them in one long list (with paging). Not ideal, but its what has been asked for.

4. rye
Member
Join Date
Dec 1969
Posts
67

## RE: Reordering A 2D Array

Hello once again.<BR>Just to follow up my original question and the very helpfull answer I was given...<BR><BR>Im having some difficultly in combining a paging system into the array ordering solution. I know this is a very open plea for help, but any advice or guidence would be much appreciated.<BR><BR>Thanks, Rye

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

## Are you storing...

...the array as a Session variable???<BR><BR>If so, then just calculate the start and end points for each page with a little math. The MOD operator is a wonderful invention!<BR><BR>&#060;%<BR>startPoint = Session("StartPoint")<BR>If startPoint &#060;&#062; "" Then<BR>&nbsp; &nbsp; startPoint = CINT( startPoint )<BR>&nbsp; &nbsp; recordArray = Session("recordArray")<BR>Else<BR>&nbsp; &nbsp; ... read in the recordset and create the array ...<BR>&nbsp; &nbsp; recordArray = RS.GetRows<BR>&nbsp; &nbsp; startPoint = ... get the random start position ...<BR>&nbsp; &nbsp; Session("StartPoint") = startPoint<BR>End If<BR><BR>arraySize = UBound( recordArray, 2 ) + 1<BR><BR>&#039; now calculate the start point for *this page*:<BR>pageStart = ( startPoint + (pageNumber * pageSize) ) MOD arraySize<BR><BR>&#039; and the end point<BR>&#039; the loopEnd is because FOR...NEXT is limited<BR>&#039; pageEnd (maybe never used?) is the *actual* last record<BR>loopEnd = pageStart + pageSize - 1<BR>pageEnd = loopEnd MOD arraySize<BR><BR>&#039; and now loop for one page:<BR>For counter = pageStart To loopEnd<BR>&nbsp; &nbsp; &#039; in case we reach end of array and wrap around, we...<BR>&nbsp; &nbsp; curRec = counter MOD arraySize &#039; limit the counter!<BR>&nbsp; &nbsp; &#039; now get the fields for the current record!<BR>&nbsp; &nbsp; &#039; (examples are for concept only )<BR>&nbsp; &nbsp; userName = arrayData( 0, curRec )<BR>&nbsp; &nbsp; userEMail = arrayData( 1, curRec )<BR>&nbsp; &nbsp; ... create the HTML ...<BR>Next<BR>...<BR>%&#062;<BR><BR>This is a *MUCH* better solution than the one I gave you a month ago. I guess I got smarter in a month, huh? &#060;sickly grin /&#062;<BR><BR>Dunno why I didn&#039;t think of using a single FOR...NEXT and the MOD operator last time! Doh.<BR><BR>

6. rye
Member
Join Date
Dec 1969
Posts
67

## RE: Are you storing...

Bill, you are an absolute Star! Thanks :)<BR>It works perfectly.<BR><BR>Thanks so much for your all of your help, its very much appreciated.<BR><BR>The MOD function is something I havent really touched yet (apart from alternating colours for table rows). Ill definitely look into it a lil further now.

7. rye
Member
Join Date
Dec 1969
Posts
67

## RE: Are you storing...

The reason behind this query is that I do have a list of 100 or so records - details of local accommodation, ie hotels, guesthouses etc - and need to present them on the web in a fashion that is both acceptable to the accommodation providers and usable to the web site viewers.<BR><BR>For example, if i list them alphabetically, A to Z, the owner of the Zebra Hotel (made up) is not going to be too happy about always being on the last page, but our visitors will be able to find a specific hotel easily.<BR><BR>If I list them completely at random, the accommodation providers would be over the moon because they all stand just as good a chance as each other of being featured on the first page, but the downside is, without the alphabetical ordering our users may have to search through every page to find a specific record.<BR><BR>The solution you came up with is going to make both groups very happy. :)<BR><BR>Cheers.

#### Posting Permissions

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