Click to See Complete Forum and Search --> : Sorting multiple or two-dimensional arrays


web-eagle
04-23-2003, 09:45 PM
I’m not getting it. (duh)

I want to have a group of images with links, which are displayed “shuffled” each time the page loads. That is I want the whole group displayed, but always in a different, random order.

I’ve tried populating a two-dimensional array in various ways, but so far haven’t gotten it to work. For now I’ve just resorted to three separate arrays.

The first (pos) array will contain a random number (0-1), which will be the sort key for all arrays. But I don’t know how (or if it’s possible) to cause the img and lnk arrays to follow the pos array. (One language I’ve used had a TagArray option. Does anything like that exist in JS?) Do I just need to give up and code a bubble sort and manually follow the other two arrays?


pos = new Array(5)
img = new Array(5)
lnk = new Array(5)
pos[0] = Math.random()
pos[1] = Math.random()
pos[2] = Math.random()
pos[3] = Math.random()
pos[4] = Math.random()
img[0] = "img1.gif"
img[1] = "img2.gif"
img[2] = "img3.gif"
img[3] = "img4.gif"
img[4] = "img5.gif"
lnk[0] = "link1.htm"
lnk[1] = "link2.htm"
lnk[2] = "link3.htm"
lnk[3] = "link4.htm"
lnk[4] = "link5.htm"


I did try concatenating the arrays, giving me something like "0.4363734 img1.gif link1.htm", which is sortable, but I'm not sure how to parse that.

Can anybody give me some ideas? I did a forum search, and a couple of similar questions came up in March, but they don’t seem to apply here.

Thanks for any and all help.

web-eagle
04-23-2003, 10:14 PM
Yep, that does the trick!

Thanks!;)

web-eagle
04-24-2003, 01:49 AM
I’m not an idiot. And I am VERY good at spotting algorithms. I’ve been programming for a just little while now (since the mid-70’s), but JS syntax (if you can call it that) makes almost no sense. I used to take flack for programming in BASIC because “it’s an unstructured language”. From what I can see, JS is a poster child for lack of structure.

I am (I assume) using the “default” sort, and it appears to me to be doing the job I need it to do, since the first element in each row is the sort key. I’ve been playing with it for a couple hours now, and the numbers are always in the order I’m looking for.

I’ve also tried some variations. (position[0].sort(), position.sort(2), position.sort(position[1]) - doesn’t seem to matter. You might give me a clue here.)

I know what I need to do. I look at the references to see how to do it. If the references aren’t clear, I look for other references. Finally, I look to someone who has more experience than I do. I am grateful for your assistance. Your example was a much better illustration than the material I had.

http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/obj.html#1009661

As it now stands, I am still unable to reference directly the elements of the array, but I plan to use the shift method to assign the elements to individual variables.

for (var n=0;n<5;n++){
var test1 = position[n].shift()
var test2 = position[n].shift()
var test3 = position[n].shift()
do stuff
do stuff
do stuff
)

I’m quite certain that someone with more experience would have a more elegant way of accessing these elements, but this is where I am. I would continue to be grateful for your further assistance, but I’ve also noted that many regulars here are quick to point out that they don’t appreciate doing other people’s work for them. That makes me fairly “skittish” about asking a question at all, unless I can somehow “prove” I’ve done the time.

I will “muddle through” until I am stumped again.

And again, thank you. I truly am grateful for your help.

web-eagle
04-24-2003, 11:33 AM
Thank you.

That explains why myArray[0,1] wasn’t working.:mad:

I had looked at the tutorial linked by your tagline, and a couple others, but nothing I saw made as much sense (to me) as your simple example.

I appreciate your “educational purposes”, and I’m still looking at the code to figure out exactly how it changes things.

There will probably be more questions in the future. If you remember me at all, remember that I HATE asking for help (probably not a good personality trait;) ), and that asking for help is always a LAST resort.

Have a good one.

WE