Sorting a two dim array

B

bg_ie

Hi,

I have an array as follows -

var MultiArray = new Array(2);
MultiArray [0] = new Array(num);
MultiArray [1] = new Array(num);

I've tried randomizing this array using the following but its obviously
wrong -

for (i=0;i<noOfVerbs;i++) {

MultiArray[0]=MultiArray[0].sort(randomSort);
MultiArray[1]=MultiArray[1].sort(randomSort);

}

function randomSort(w1,w2)
{
return Math.floor(Math.random()*3)-1;
}

I want to randomize the array, but if MultiArray[0][1] becomes
MultiArray[0][13], then I want MultiArray[1][1] to become
MultiArray[1][13], and so on.

How can I do this?

Thanks,

Barry.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Fri, 8 Jul 2005 10:29:50, seen in (e-mail address removed) posted :
I have an array as follows -

var MultiArray = new Array(2);
MultiArray [0] = new Array(num);
MultiArray [1] = new Array(num);

I've tried randomizing this array using the following but its obviously
wrong -

for (i=0;i<noOfVerbs;i++) {

MultiArray[0]=MultiArray[0].sort(randomSort);
MultiArray[1]=MultiArray[1].sort(randomSort);

}

function randomSort(w1,w2)
{
return Math.floor(Math.random()*3)-1;
}

I want to randomize the array, but if MultiArray[0][1] becomes
MultiArray[0][13], then I want MultiArray[1][1] to become
MultiArray[1][13], and so on.

How can I do this?

Rather a confusing description.

You should not randomise by random sorting; there are better, easy
methods. FAQ 4.22 has a link on shuffling.

The .sort routine is written in the expectation that the result of
randomSort(w1,w2) depends on, and only on, w1,w2, reproducibly. FAQ.

For your purpose, if I understand it, which I doubt, you should shuffle
the outer 1D array, in which each entry is itself an array. That means
either redesigning your algorithm to use the indexes in reverse order,
or transposing, shuffling, and re-transposing.

Another approach would be to deal - FAQ 4.22 has a link on dealing -
integers into an array of size num, then to copy your 2D array with the
position of each element in the new sub-arrays given by lookup in the
once-dealt array.

You could write your own random function, initialisable by yourself, so
that after each initialisation it would generate the same sequence, thus
being able to random-sort each sub-array identically. But not a good
idea. FAQ.
 
L

Lee

(e-mail address removed) said:
Hi,

I have an array as follows -

var MultiArray = new Array(2);
MultiArray [0] = new Array(num);
MultiArray [1] = new Array(num);

I've tried randomizing this array using the following but its obviously
wrong -

for (i=0;i<noOfVerbs;i++) {

MultiArray[0]=MultiArray[0].sort(randomSort);
MultiArray[1]=MultiArray[1].sort(randomSort);

}

function randomSort(w1,w2)
{
return Math.floor(Math.random()*3)-1;
}

I want to randomize the array, but if MultiArray[0][1] becomes
MultiArray[0][13], then I want MultiArray[1][1] to become
MultiArray[1][13], and so on.

How can I do this?

If I understand what you want, each element of MultiArray[0]
should still be lined up with the corresponding element of
MultiArray[1] after both arrays have been sorted.

That's an excellent indication that you should really be
using an array of custom Objects, each with two fields.

for example, if MultiArray[0] is a list of names, and
MultiArray[1] is the id number that goes with those names:

var person = [
{name:"Jim", id:"0123"},
{name:"Sue", id:"4321"},
{name:"Bob", id:"9331"}
];

Then you can sort or shuffle this single array and keep the
two fields together. It also has the advantage that the
variable names can be much more meaningfull than
"MultiArray[1][3]" (which would be referenced as "person[3].id",
in my example).
 
L

Lee

Danny said:
Why the shuffling anyway? I gather you want to output certain array
element in so-called random fashion? For that, I just use Math.random()
times a base10 amount and round it, to make it Integer and then get its
modulus against the .lenght of it,
ARRNAME[(Math.round(Math.random()*1000))%ARRNAME.length] or so.

I'm just really being a pest, tonight.
Don't let me discourage you too much.

That's not a good way to generate random numbers.
Unless the length divides evenly into 1000, some of your elements
will be selected more often than others. If the multiplier is
very large, relative to the array length, the difference may
not be enough to worry about, but 1000 is not very large.

ARRNAME[Math.floor(Math.random()*ARRNAME.length)]

will select the elements with an even distribution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top